如何设置中选择一个按钮的文本样式? [英] How to set the text style of a button in selectors?

查看:99
本文介绍了如何设置中选择一个按钮的文本样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按钮选择允许定义背景,按钮的每个状态,但怎样才能按钮的文本样式为每个国家定义的?我的目标是禁用时变为灰色的按钮。所以我设置一个灰色的背景,按钮的禁用状态,但只有这样,我发现也变灰的文字是改变它的颜色动态,当我禁用按钮...

任何人都可以帮忙吗?

感谢

 <样式名称=srp_button父=@安卓风格/ Widget.Button>
    <项目名称=机器人:背景> @可绘制/ btn_default< /项目>
    <项目名称=机器人:文字颜色>#FFFFFF< /项目> <! - 如何指定不同的颜色,不同的状态? - >
    <! - 更多的东西 - >
< /风格>
 

绘制/ btn_default.xml

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>

    <项目安卓state_enabled =真正的><形状>
            [固体机器人:颜色=@色/粉红色/>
            <边角机器人:半径=6DP/>
        < /形状>< /项目>
    <项目><形状>
            [固体机器人:颜色=#000000/>
            <边角机器人:半径=6DP/>
        < /形状>< /项目>

< /选择器>
 

解决方案

嘛!

您可以定义文本颜色为所有4州,同样为您定义背景的按钮。例如:

文件名: /res/color/text_color.xml

 < XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目安卓state_enabled =假的Andr​​oid版本:颜色=#440000/>
    <项目安卓state_focused =真正的机器人:颜色=#004400/>
    <项目的android:STATE_ pressed =真正的机器人:颜色=#000044/>
    <项目的android:颜色=#444/>
< /选择器>
 

取这个文件放到你的按钮风格是这样的:

 <样式名称=srp_button父=@安卓风格/ Widget.Button>
    <项目名称=机器人:背景> @可绘制/ btn_default< /项目>
    <项目名称=机器人:文字颜色> @色/ text_color< /项目> <! - 在这里指定文本颜色 - >
< /风格>
 

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...

Anybody can help?

Thanks

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

解决方案

Well!

You can define the Text color for all 4 state, similarly as you defined background for the Button. For example:

file name: /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>

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆