【问题标题】:Text color selector doesn't work on android 4文本颜色选择器在 android 4 上不起作用
【发布时间】:2017-01-24 14:13:01
【问题描述】:

我尝试为所有按钮应用简单的文本颜色选择器。我覆盖默认按钮样式并在我的应用程序主题 (AppTheme) 中使用它。它在 android 5 及更高版本上按预期工作,但在 android 4 上不起作用。查看我的代码。

styles.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:buttonStyle">@style/Button</item>
    </style>

    <style name="Button" parent="Widget.AppCompat.Button">
        <item name="android:textColor">@drawable/btn_text_color_selector</item>
    </style>

</resources>

btn_text_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimary" android:state_enabled="false"></item>
    <item android:color="@color/colorAccent"></item>
</selector>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Kalės vaikas" />

【问题讨论】:

  • 嘿,我没看到,你是如何在按钮中设置颜色的,检查你的帖子
  • AppTheme 更改默认按钮样式。这是我的应用程序的主题。

标签: android button android-text-color


【解决方案1】:

不使用btn_text_color_selector.xml,不如直接添加android:textColor="@color/colorPrimary":

喜欢这个

 <Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
android:textColor="@color/colorPrimary"
        android:layout_height="wrap_content"
        android:text="Kalės vaikas" />

这将维护所有平台。

【讨论】:

  • 因为那将是样板代码。我需要将该行添加到我的应用程序的每个按钮中。这是多余的。此外,文本颜色不仅仅是一种颜色,它是一个选择器,这意味着文本会根据按钮状态改变颜色。
  • 确实是@Elgis,这是多余的,但它会执行您想要的,有时我们会考虑输出,
猜你喜欢
  • 2012-08-18
  • 1970-01-01
  • 2012-12-14
  • 1970-01-01
  • 2015-12-08
  • 1970-01-01
  • 2016-02-12
  • 1970-01-01
  • 2014-10-31
相关资源
最近更新 更多