【发布时间】:2022-03-15 15:03:37
【问题描述】:
我有 TextView。我想以大写模式显示文本。 有大写的属性吗? 只是我在 strings.xml 中有文本,我需要在几个地方使用该行作为小写和大写。
【问题讨论】:
我有 TextView。我想以大写模式显示文本。 有大写的属性吗? 只是我在 strings.xml 中有文本,我需要在几个地方使用该行作为小写和大写。
【问题讨论】:
在布局 XML 中,您可以在 TextView 上设置 android:textAllCaps 属性:
<TextView android:textAllCaps="true"></TextView>
【讨论】:
使用String类方法同时将文本设置为TextView as
tv.setText(strings.toUpperCase());
查看this 帖子以进行详细讨论。
编辑: 这早在 API 14 之前就已得到回答。正如此处提到的其他答案,textView.isAllCaps = true 及其 java 方法是从 TextView 引入的。请使用这些。
【讨论】:
以编程方式使用它,例如 textView.setAllCaps(false) 原样和 textView.setAllCaps(true) 大写
android:textAllCaps="true" 用于 XML 布局
【讨论】:
科特林:
textView.isAllCaps = true
【讨论】:
TextView 没有所有大写的getter,Kotlin 没有为您提供属性,您仍然需要调用setAllCaps(true)。
TextView#isAllCaps,但它在QA 的系统上给出了lint 错误,并使他更改为TextView#setAllCaps()。但是给我 lint 警告
maxSdkVersion 的 API 版本小于 28 将给出 TextView#isAllCaps 的 lint 错误。 28 及以上将运行顺利
对于 XML 方法,您不想使用 android:capitalize,因为它是在文本输入期间使用的。相反,请使用textAllCaps。如果您的字符串被声明为小写,那么在每个 TextView 的基础上在大写和小写之间切换非常简单。
【讨论】:
使用它
<TextView android:textAllCaps="true"></TextView>
【讨论】:
我相信没有这样的属性,但你可以使用
textView.setText(text.toUpperCase());
也发现了这个,不过我自己从来没有测试过
android:capitalize="characters"
【讨论】:
TextView中使用android:capitalize="characters"
试试这个。
大写
textView.setText(getResources().getString(R.string.app_name).toUpperCase());
小写
textView.setText(getResources().getString(R.string.app_name).toLowerCase());
【讨论】:
在布局文件中设置 android:textAllCaps="true"。
编辑: 如果您使用的是 kotlin,请使用 .capliatlized() 方法 https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/capitalize.html
【讨论】:
您可以创建一个从 TextView 派生的自定义视图并覆盖 setText 方法以大写。
【讨论】:
您可以在 android xml 文件中轻松做到这一点。 您应该将此行添加到 TextView。
<TextView
android:textAllCaps="true"/>
如果你想在代码中这样做,你可以这样做:
textView.setText(strings.toUpperCase());
【讨论】:
获取 TextView 的引用并在对象上调用此方法
textView.setAllCaps(true);
【讨论】: