Селектор цвета текста не работает на Android 4

Я пытаюсь применить простой текстовый селектор цвета для всех моих кнопок. Я переопределяю стиль кнопок по умолчанию и использую его в своей теме приложения (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" />

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" />

Это будет поддерживать все платформы.

Другие вопросы по тегам