TJ *_*ers 1 android button textcolor
使用Android Studio。我正在尝试使用选择器来使按钮中的文本从白色变为深灰色。仅文本,而不是按钮的背景。
这是我的选择器xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/darker_gray" android:state_pressed="true"></item>
<item android:drawable="@android:color/white" android:state_focused="true"></item>
<item android:drawable="@android:color/white" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="@android:color/white" android:state_enabled="false"></item>
</selector>
Run Code Online (Sandbox Code Playgroud)
这是我的Button xml:
<Button
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="Maps"
android:id="@+id/buttonMaps"
android:layout_above="@+id/buttonEmail"
android:layout_marginBottom="5dp"
android:layout_centerHorizontal="true"
android:textColor="@drawable/selector" <------- **SELECTOR NOT WORKING**
android:background="#7f2f74a0" <------- **but it does work if I stick it here**
android:textSize="20sp" />
Run Code Online (Sandbox Code Playgroud)
在预览面板中,我收到一条渲染错误消息,内容为:“无法为/Users...src/main/res/drawable/selector.xml配置解析器”
我尝试了SO的许多解决方案,但没有任何效果,包括清理和重建项目。有趣的是,我很好奇并将选择器应用于按钮的背景,它可以正常工作-按钮的背景默认为白色,按下时变为灰色。
有比我更聪明的人有解决方案吗?
谢谢!
将文本选择器放在资源下的颜色文件夹中,而不是可绘制的位置。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/darker_gray" android:state_pressed="true"></item>
<item android:color="@android:color/white" android:state_focused="true"></item>
<item android:color="@android:color/white" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:color="@android:color/white" android:state_enabled="false"></item>
</selector>
Run Code Online (Sandbox Code Playgroud)
并像这样使用
<Button
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="Maps"
android:id="@+id/buttonMaps"
android:layout_above="@+id/buttonEmail"
android:layout_marginBottom="5dp"
android:layout_centerHorizontal="true"
android:textColor="@color/selector"
android:textSize="20sp" />
Run Code Online (Sandbox Code Playgroud)