更改颜色选择器/文本新的文本MaterialButton

时间:2019-02-17 10:28:07

标签: android material-components

选择按钮时,我需要自定义选择器的文本颜色或背景颜色,因为文本的颜色太相似并且文本消失了

这是我的代码:

  <android.support.design.button.MaterialButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            style="@style/Widget.MaterialComponents.Button.TextButton"
            android:text="@string/lbl_forgotten_password"
            android:textColor="@color/gray_text_1"
            android:textSize="14sp" />

外观如下:

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用selector解决此问题。

在drawable文件夹中创建一个新的xml文件。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="@color/text_color" android:drawable="@drawable/button_bg"/>
    <item android:color="@color/text_normal" />
</selector>

之后

<android.support.design.button.MaterialButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/your_xml_file_name"
        android:text="@string/lbl_forgotten_password"
        android:textSize="14sp" />
相关问题