Button默认英文文言大写问题

本文解析了Android中Button控件自动将文本转换为大写的原因,并提供了四种解决方案,包括直接在Button上设置属性、创建样式、修改主题及替换控件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

在Android的开发中,布局文件中经常用到<Button>这个控件,就遇到个问题,英文文言不管怎么写,最后显示都会是全部大写的,看起了很不方便,如下图:
在这里插入图片描述




原因分析:

其父类<TextView>控件就没有这个问题了,说明<Button>在继承之后修改了内部属性,百度了一下,迎刃而解,添加下面属性即可:

android:textAllCaps="false"

在布局中,此属性为文言全部大写(英文),如果想去掉,把该属性设置为false即可,那么这个属性是从哪来的呢?

首先需要了解Android默认属性是从哪获取的,这就需要知道Theme这个概念了,在这就不做扩展了。

从AndroidManifest.xml的application标签找到theme属性,根据theme属性,一步步查找。

<application
  android:allowBackup="true"
  android:fullBackupContent="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:roundIcon="@mipmap/ic_launcher_round"
  android:theme="@style/AppTheme">
<style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar">
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="Theme.AppCompat.DayNight.DarkActionBar" parent="Theme.AppCompat.Light.DarkActionBar"/>
<style name="Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light.DarkActionBar"/>
<style name="Base.Theme.AppCompat.Light.DarkActionBar" parent="Base.Theme.AppCompat.Light">
<style name="Base.Theme.AppCompat.Light" parent="Base.V7.Theme.AppCompat.Light">

这里需要说明一下,下面那条为属性,是上面那个style的parent的内部属性,此处位置需要找好。

<item name="android:textAppearanceButton">@style/TextAppearance.AppCompat.Widget.Button</item>
<style name="TextAppearance.AppCompat.Widget.Button" parent="Base.TextAppearance.AppCompat.Widget.Button"/>
<style name="Base.TextAppearance.AppCompat.Widget.Button" parent="TextAppearance.AppCompat.Button"/>
<style name="TextAppearance.AppCompat.Button" parent="Base.TextAppearance.AppCompat.Button"/>

最终找到如下两个设置,确认出android:textAllCaps属性默认设置为true,导致文言一直为大写。

1.V21之前的版本:

<style name="Base.TextAppearance.AppCompat.Button">
  <item name="android:textSize">@dimen/abc_text_size_button_material</item>
  <item name="android:textAllCaps">true</item>
  <item name="android:textColor">?android:textColorPrimary</item>
</style>

2.V21之后的版本:

<style name="Base.TextAppearance.AppCompat.Button" parent="android:TextAppearance.Material.Button"/>
<style name="TextAppearance.Material.Button">
  <item name="textSize">@dimen/text_size_button_material</item>
  <item name="fontFamily">@string/font_family_button_material</item>
  <item name="textAllCaps">true</item>
  <item name="textColor">?attr/textColorPrimary</item>
</style>

 




解决方法:

知道了原因,解决起来就很简单了,在这呢,提供了几个方案供大家选择:

方案1:谁不好使解决谁,逐个确认效果
<Button
  android:id="@+id/btn_answer1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="Button"
  android:textAllCaps="false" />

方案2:同类问题一起解决,共通属性设置
<style name="StyleButton">
  <item name="android:layout_width">match_parent</item>
  <item name="android:layout_height">wrap_content</item>
  <item name="android:layout_margin">5dp</item>
  <item name="android:textAllCaps">false</item>
  <item name="android:gravity">center</item>
</style>

方案3:同步消息,主题设置
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="colorAccent">@color/colorAccent</item>
  <item name="android:textAllCaps">false</item>
</style>

方案4:惹不起但躲得起,换控件
<TextView
  android:id="@+id/btn_answer1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="Button" />

前三种为较常见的方案,但是出入个别的原因呢,我比较喜欢第四种方案,因为<Button>本身就继承于<TextView>,在使用中,差异性还是挺小的。

 




遗留问题:

在追踪theme主题时,偶尔会使用下面这个主题,没有路径去追踪相关属性,如何判断其属性呢?

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <item name="android:windowContentOverlay">@null</item>
  <item name="android:windowNoTitle">true</item>
  <item name="android:textAllCaps">false</item>
</style>
<style name="Theme.AppCompat.Light.NoActionBar">
  <item name="windowActionBar">false</item>
  <item name="windowNoTitle">true</item>
</style>


曼陀罗华:无尽的思念,绝望的爱情,天堂的来信

曼珠沙华:无尽的爱情,死亡的前兆,地狱的召唤



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值