如何在选择器中设置按钮的文本样式? [英] How to set the text style of a button in selectors?
问题描述
按钮选择器允许为按钮的每个状态定义背景,但是如何为每个状态定义按钮的文本样式?我的目标是在禁用时使按钮变灰.因此,我将灰色背景设置为按钮的禁用状态,但我发现使文本变灰的唯一方法是在禁用按钮时动态更改其颜色...
Button selectors allows to define a background for each state of the button, but how can the button's text style be defined for each state? My objective is to grey out a button when disabled. So I set a greyed out background to the disable state of the button, but the only way I found to also grey out the text is to change it's color dynamically when I disable the button...
有人可以帮忙吗?
谢谢
<style name="srp_button" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/btn_default</item>
<item name="android:textColor">#ffffff</item> <!-- How to specify different color for different state? -->
<!-- more stuff -->
</style>
drawable/btn_default.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"><shape>
<solid android:color="@color/pink" />
<corners android:radius="6dp" />
</shape></item>
<item><shape>
<solid android:color="#000000" />
<corners android:radius="6dp" />
</shape></item>
</selector>
推荐答案
好吧!
您可以为所有 4 个状态定义文本颜色,类似于为 Button
定义背景.例如:
You can define the Text color for all 4 state, similarly as you defined background for the Button
. For example:
文件名:/res/color/text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#440000"/>
<item android:state_focused="true" android:color="#004400"/>
<item android:state_pressed="true" android:color="#000044"/>
<item android:color="#444"/>
</selector>
将此文件放入您的 Button
样式中,如下所示:
Take this file into your Button
Style like this:
<style name="srp_button" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/btn_default</item>
<item name="android:textColor">@color/text_color</item> <!-- Here is specified text color -->
</style>
这篇关于如何在选择器中设置按钮的文本样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!