无法在Android 21及以下版本中覆盖按钮样式中的textAllCaps属性

9

这是我设置按钮的方法。

<Button
    android:id="@+id/button_login"
    style="@style/ButtonStyle"
    android:text="@string/button_login" />

这是我在values文件夹中的样式。

<style name="ButtonStyle" parent="ButtonStyleBase" />

<style name="ButtonStyleBase">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginTop">@dimen/padding</item>
    <item name="android:textSize">@dimen/font_regular</item>
    <item name="android:textColor">@color/text_regular</item>
    <item name="android:background">@drawable/shape_clickable</item>
</style>

这是我在values-v21文件夹中的样式

<style name="ButtonStyle" parent="ButtonStyleBase">
    <item name="textAllCaps">false</item>
    <item name="android:textColor">#000000</item>
</style>

但是按钮上的文本总是大写的。如果我直接在按钮上设置它,它会恢复正常。我更改了颜色以查看是否正在使用api 21的样式,它确实被使用了,按钮文本颜色在api 21上变为黑色。我知道默认主题将textAllCaps设置为按钮的true,因为Google认为这很酷,但它不应该优先考虑我的样式吗?
编辑:不要紧,我忘记在样式上写"android:"了。
4个回答

22

我遇到了同样的问题,以下是解决方法:

<style name="Theme.CustomTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:textAppearanceButton">@style/CustomTheme.ButtonTextAppearance</item>
</style>

<style name="CustomTheme.ButtonTextAppearance" parent="@style/Base.TextAppearance.AppCompat.Button">
    <item name="textAllCaps">false</item>
    <item name="android:textAllCaps">false</item>
</style>

希望这可以帮到你。


1
styles.xml 中,
将按钮样式包含在您的 AppTheme 中(将在 ApplicationActivity 中使用)。
    <style name="AppTheme" parent="@style/Theme.AppCompat.Light">
<item name="android:buttonStyle">@style/MyButton</item>    
</style>

创建按钮样式
<style name="MyButton" parent="Widget.AppCompat.Button">
        <item name="textAllCaps">false</item>
        <item name="android:textAllCaps">false</item>
    </style>

如果您发现任何问题,请告诉我。


0

你在values-v21文件夹中的样式是正确的

<style name="ButtonStyle" parent="ButtonStyleBase">
    <item name="textAllCaps">false</item>
    <item name="android:textColor">#000000</item>
</style>

但是在 Api 等级高于 21 的同时,您的样式需要粘贴到 values-v19 和 style 文件夹 xml 中。


0
请确保以下行中在 "textAllCaps" 之前没有 "android:": <item name="textAllCaps" tools:targetApi="ice_cream_sandwich">false</item>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接