有没有办法将 TextView 的样式设置为大写所有字母?
我希望能够为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我认为这是一个相当合理的请求,但看起来你现在不能这样做< /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.
android:textAllCaps 怎么样?
What about android:textAllCaps?
通过在支持较旧 API(少于 14 个)的 Android 应用中使用 AppCompat
textAllCaps
AppCompat 附带一个名为 CompatTextView 的 UI 小部件,它是一个自定义 TextView 扩展,它添加了对 textAllCaps 的支持
对于较新的 android API > 14 你可以使用:
一个简单的例子:
来源:developer.android
更新:
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 :
A simple example:
Source:developer.android
Update:
确实非常令人失望的是,您无法使用样式 (
- true
) 或在每个 XML 布局文件上使用 textAllCaps 属性,唯一的方法是在执行 textViewXXX.setText(theString) 时对每个字符串使用 theString.toUpperCase() 。就我而言,我不想在代码中到处都有 theString.toUpperCase() ,而是希望有一个集中的地方来执行此操作,因为我有一些 Activity 并列出了带有 TextView 的项目布局,这些布局应该在哪里一直大写(标题),而其他人则没有……所以……有些人可能认为这是一种矫枉过正,但我创建了自己的 CapitalizedTextView 类,扩展 android.widget.TextView 并覆盖 setText 方法,动态将文本大写。
至少,如果设计发生变化或者我需要在未来版本中删除大写文本,我只需要在布局文件中更改为普通 TextView 即可。
现在,请考虑到我这样做是因为应用程序的设计者实际上希望该文本(标题)在整个应用程序中都以大写字母显示,无论原始内容的大小写如何,而且我还有其他普通的 TextView,其中大小写与实际内容一起出现。
这就是这个类:
无论何时需要使用它,只需在 XML 布局中使用所有包来声明它:
有些人会认为在 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:
And whenever you need to use it, just declare it with all the package in the XML layout:
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.
我提出了一个与 RacZo 类似的解决方案,因为我还创建了一个
TextView
的子类,它负责将文本设置为大写。不同之处在于,我没有重写
setText()
方法之一,而是使用了与TextView
在 API 14+ 上实际执行的操作类似的方法(位于我的观点是更清洁的解决方案)。如果您查看源,您将看到
setAllCaps()
的实现:AllCapsTransformationMethod
类(当前)不是公共的,但源代码也是 可用。我稍微简化了该类(删除了 setLengthChangesAllowed() 方法),所以完整的解决方案是这样的: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 theTextView
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()
:The
AllCapsTransformationMethod
class is not (currently) public, but still, the source is also available. I've simplified that class a bit (removed thesetLengthChangesAllowed()
method), so the complete solution is this:基本上,在 XML 文件的 TextView 中写入以下内容:
Basically, write this in TextView of XML file:
似乎有移动键盘设置的权限,所以最简单的方法是:
希望这会起作用
It seems like there is permission on mobile keypad setting, so the easiest way to do this is:
hope this will work
PixlUI 项目允许您在任何文本视图或文本视图的子类中使用textAllCaps,包括:
按钮,
编辑文本
自动完成编辑文本
复选框
单选按钮
和其他几个。
您需要使用 pixlui 版本而不是 Android 源代码创建文本视图,这意味着您必须这样做:
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:
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.
对于撰写
For compose