有没有办法将 TextView 的样式设置为大写所有字母?

发布于 2024-10-07 14:16:40 字数 309 浏览 2 评论 0原文

我希望能够为 TextView 分配一个 xml 属性或样式,该属性或样式将生成所有大写字母中的任何文本。

属性 android:inputType="textCapCharacters"android:capitalize="characters" 不执行任何操作,看起来它们是用于用户输入的文本,而不是 TextView< /代码>。

我想这样做,这样我就可以将风格与内容分开。我知道我可以通过编程来完成此操作,但我再次希望保持内容和代码的风格。

I would like to be able to assign a xml attribute or style to a TextView that will make whatever text it has in ALL CAPITAL LETTERS.

The attributes android:inputType="textCapCharacters" and android:capitalize="characters" do nothing and look like they are for user inputed text, not a TextView.

I would like to do this so I can separate the style from the content. I know I could do this programmically but again I want keep style out of the content and the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

我的黑色迷你裙 2024-10-14 14:16:40

我认为这是一个相当合理的请求,但看起来你现在不能这样做< /a>.

更新

您现在可以使用
textAllCaps
强制全部大写。

I though that was a pretty reasonable request but it looks like you can't do it at this time.

Update

You can now use
textAllCaps
to force all caps.

巴黎夜雨 2024-10-14 14:16:40

通过在支持较旧 API(少于 14 个)的 Android 应用中使用 AppCompat textAllCaps

AppCompat 附带一个名为 CompatTextView 的 UI 小部件,它是一个自定义 TextView 扩展,它添加了对 textAllCaps 的支持

对于较新的 android API > 14 你可以使用:

android:textAllCaps="true"

一个简单的例子:

<android.support.v7.internal.widget.CompatTextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:textAllCaps="true"/>

来源:developer.android

更新:

碰巧 CompatTextView 被 AppCompatTextView 取代
最新的 appcompat-v7 库 ~ Eugen Pechanec

By using AppCompat textAllCaps in Android Apps supporting older API's (less than 14)

There is one UI widgets that ships with AppCompat named CompatTextView is a Custom TextView extension that adds support for textAllCaps

For newer android API > 14 you can use :

android:textAllCaps="true"

A simple example:

<android.support.v7.internal.widget.CompatTextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:textAllCaps="true"/>

Source:developer.android

Update:

As it so happens CompatTextView was replaced by AppCompatTextView in
latest appcompat-v7 library ~ Eugen Pechanec

寻找一个思念的角度 2024-10-14 14:16:40

确实非常令人失望的是,您无法使用样式 (true) 或在每个 XML 布局文件上使用 textAllCaps 属性,唯一的方法是在执行 textViewXXX.setText(theString) 时对每个字符串使用 theString.toUpperCase() 。

就我而言,我不想在代码中到处都有 theString.toUpperCase() ,而是希望有一个集中的地方来执行此操作,因为我有一些 Activity 并列出了带有 TextView 的项目布局,这些布局应该在哪里一直大写(标题),而其他人则没有……所以……有些人可能认为这是一种矫枉过正,但我​​创建了自己的 CapitalizedTextView 类,扩展 android.widget.TextView 并覆盖 setText 方法,动态将文本大写。

至少,如果设计发生变化或者我需要在未来版本中删除大写文本,我只需要在布局文件中更改为普通 TextView 即可。

现在,请考虑到我这样做是因为应用程序的设计者实际上希望该文本(标题)在整个应用程序中都以大写字母显示,无论原始内容的大小写如何,而且我还有其他普通的 TextView,其中大小写与实际内容一起出现。

这就是这个类:

package com.realactionsoft.android.widget;

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.ViewTreeObserver; 
import android.widget.TextView;


public class CapitalizedTextView extends TextView implements ViewTreeObserver.OnPreDrawListener {

    public CapitalizedTextView(Context context) {
        super(context);
    }

    public CapitalizedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CapitalizedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text.toString().toUpperCase(), type);
    }

}

无论何时需要使用它,只需在 XML 布局中使用所有包来声明它:

<com.realactionsoft.android.widget.CapitalizedTextView 
        android:id="@+id/text_view_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

有些人会认为在 TextView 上设置文本样式的正确方法是使用 SpannableString,但我认为这将是一个更大的矫枉过正,更不用说更消耗资源,因为您将实例化另一个类而不是 TextView。

It is really very disappointing that you can't do it with styles (<item name="android:textAllCaps">true</item>) or on each XML layout file with the textAllCaps attribute, and the only way to do it is actually using theString.toUpperCase() on each of the strings when you do a textViewXXX.setText(theString).

In my case, I did not wanted to have theString.toUpperCase() everywhere in my code but to have a centralized place to do it because I had some Activities and lists items layouts with TextViews that where supposed to be capitalized all the time (a title) and other who did not... so... some people may think is an overkill, but I created my own CapitalizedTextView class extending android.widget.TextView and overrode the setText method capitalizing the text on the fly.

At least, if the design changes or I need to remove the capitalized text in future versions, I just need to change to normal TextView in the layout files.

Now, take in consideration that I did this because the App's Designer actually wanted this text (the titles) in CAPS all over the App no matter the original content capitalization, and also I had other normal TextViews where the capitalization came with the the actual content.

This is the class:

package com.realactionsoft.android.widget;

import android.content.Context; 
import android.util.AttributeSet; 
import android.view.ViewTreeObserver; 
import android.widget.TextView;


public class CapitalizedTextView extends TextView implements ViewTreeObserver.OnPreDrawListener {

    public CapitalizedTextView(Context context) {
        super(context);
    }

    public CapitalizedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CapitalizedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        super.setText(text.toString().toUpperCase(), type);
    }

}

And whenever you need to use it, just declare it with all the package in the XML layout:

<com.realactionsoft.android.widget.CapitalizedTextView 
        android:id="@+id/text_view_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

Some will argue that the correct way to style text on a TextView is to use a SpannableString, but I think that would be even a greater overkill, not to mention more resource-consuming because you'll be instantiating another class than TextView.

节枝 2024-10-14 14:16:40

我提出了一个与 RacZo 类似的解决方案,因为我还创建了一个 TextView 的子类,它负责将文本设置为大写。

不同之处在于,我没有重写 setText() 方法之一,而是使用了与 TextView 在 API 14+ 上实际执行的操作类似的方法(位于我的观点是更清洁的解决方案)。

如果您查看源,您将看到 setAllCaps() 的实现:

public void setAllCaps(boolean allCaps) {
    if (allCaps) {
        setTransformationMethod(new AllCapsTransformationMethod(getContext()));
    } else {
        setTransformationMethod(null);
    }
}

AllCapsTransformationMethod 类(当前)不是公共的,但源代码也是 可用。我稍微简化了该类(删除了 setLengthChangesAllowed() 方法),所以完整的解决方案是这样的:

public class UpperCaseTextView extends TextView {

    public UpperCaseTextView(Context context) {
        super(context);
        setTransformationMethod(upperCaseTransformation);
    }

    public UpperCaseTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setTransformationMethod(upperCaseTransformation);
    }

    public UpperCaseTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTransformationMethod(upperCaseTransformation);
    }

    private final TransformationMethod upperCaseTransformation =
            new TransformationMethod() {

        private final Locale locale = getResources().getConfiguration().locale;

        @Override
        public CharSequence getTransformation(CharSequence source, View view) {
            return source != null ? source.toString().toUpperCase(locale) : null;
        }

        @Override
        public void onFocusChanged(View view, CharSequence sourceText,
                boolean focused, int direction, Rect previouslyFocusedRect) {}
    };
}

I've come up with a solution which is similar with RacZo's in the fact that I've also created a subclass of TextView which handles making the text upper-case.

The difference is that instead of overriding one of the setText() methods, I've used a similar approach to what the TextView actually does on API 14+ (which is in my point of view a cleaner solution).

If you look into the source, you'll see the implementation of setAllCaps():

public void setAllCaps(boolean allCaps) {
    if (allCaps) {
        setTransformationMethod(new AllCapsTransformationMethod(getContext()));
    } else {
        setTransformationMethod(null);
    }
}

The AllCapsTransformationMethod class is not (currently) public, but still, the source is also available. I've simplified that class a bit (removed the setLengthChangesAllowed() method), so the complete solution is this:

public class UpperCaseTextView extends TextView {

    public UpperCaseTextView(Context context) {
        super(context);
        setTransformationMethod(upperCaseTransformation);
    }

    public UpperCaseTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setTransformationMethod(upperCaseTransformation);
    }

    public UpperCaseTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTransformationMethod(upperCaseTransformation);
    }

    private final TransformationMethod upperCaseTransformation =
            new TransformationMethod() {

        private final Locale locale = getResources().getConfiguration().locale;

        @Override
        public CharSequence getTransformation(CharSequence source, View view) {
            return source != null ? source.toString().toUpperCase(locale) : null;
        }

        @Override
        public void onFocusChanged(View view, CharSequence sourceText,
                boolean focused, int direction, Rect previouslyFocusedRect) {}
    };
}
静赏你的温柔 2024-10-14 14:16:40

基本上,在 XML 文件的 TextView 中写入以下内容:

android:textAllCaps="true"

Basically, write this in TextView of XML file:

android:textAllCaps="true"
川水往事 2024-10-14 14:16:40

似乎有移动键盘设置的权限,所以最简单的方法是:

editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});

希望这会起作用

It seems like there is permission on mobile keypad setting, so the easiest way to do this is:

editText.setFilters(new InputFilter[]{new InputFilter.AllCaps()});

hope this will work

﹎☆浅夏丿初晴 2024-10-14 14:16:40

PixlUI 项目允许您在任何文本视图或文本视图的子类中使用textAllCaps,包括:
按钮,
编辑文本
自动完成编辑文本
复选框
单选按钮
和其他几个。

您需要使用 pixlui 版本而不是 Android 源代码创建文本视图,这意味着您必须这样做:

<com.neopixl.pixlui.components.textview.TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        pixlui:textAllCaps="true" />

PixlUI 还允许您设置放入资产文件夹中的自定义字体/字体。

我正在开发一个 Gradle PixlUI 框架的分支,它使用 gradle 并允许指定 textAllCaps作为样式的字体,而不是像原始项目那样要求它们内联。

PixlUI project allows you to use textAllCaps in any textview or subclass of textview including:
Button,
EditText
AutoCompleteEditText
Checkbox
RadioButton
and several others.

You will need to create your textviews using the pixlui version rather than the ones from the android source, meaning you have to do this:

<com.neopixl.pixlui.components.textview.TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        pixlui:textAllCaps="true" />

PixlUI also allows you to set a custom typeface/font which you put in your assets folder.

I'm working on a Gradle fork of the PixlUI framework which uses gradle and allows one to specify textAllCaps as well as the typeface from styles rather than requiring them inline as the original project does.

彼岸花似海 2024-10-14 14:16:40

对于撰写

Text(
    text = ("your text").uppercase()
)

For compose

Text(
    text = ("your text").uppercase()
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文