Android:选择器中禁用按钮的 textColor 未显示? [英] Android: textColor of disabled button in selector not showing?

查看:19
本文介绍了Android:选择器中禁用按钮的 textColor 未显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个带有选择器的按钮,我的按钮可以有以下状态:

I am trying to make a button with a selector my button can have the following states:

  • 启用/禁用
  • 按下/未按下

根据上面提到的状态.我需要操作按钮的:

According to the states mentioned above. I need to manipulate the button's:

  • 文字颜色
  • 背景图片

按钮从我被禁用开始,所以它应该有禁用的 textColor 和禁用的按钮背景.但是我可以看到默认的 textColor(在样式中指定)并且没有背景图像!

The button starts off my being disabled so it should have the disabled textColor and the disabled button background. But I can see the default textColor (specified in style) and NO background image!

这是我的选择器 button_selector.xml

Here is my selector button_selector.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="false"
        android:state_enabled="false"
        android:textColor="#9D9FA2"
        android:drawable="@drawable/button" />    

    <item android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@drawable/button_pressed"/>

    <item android:state_pressed="true"
        android:state_enabled="false"
        android:textColor="#9D9FA2"
        android:drawable="@drawable/button"/>

    <item android:state_pressed="false"
        android:state_enabled="true"
        android:drawable="@drawable/button"/>    

</selector>

这是我在 layout.xml 中的按钮声明

And here is my button declaration in the my layout.xml

    <Button android:id="@+id/reserve_button"
        android:text="@string/reserve_button"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="20dp"
        android:paddingRight="15dp"
        android:layout_gravity="left"
        style="@style/buttonStyle"
        android:background="@drawable/button_selector" />

最后这是我的风格(我的默认 textColor 设置)

And finally this is my style (where my default textColor is set)

<?xml version="1.0" encoding="utf-8"?>

 <resources>

     <style name="buttonStyle">
      <item name="android:textStyle">bold</item>
      <item name="android:textColor">#282780</item>
      <item name="android:textSize">18sp</item>
     </style>

</resources>

请帮忙!

推荐答案

您还需要创建一个 ColorStateList 用于识别不同状态的文本颜色.

You need to also create a ColorStateList for text colors identifying different states.

执行以下操作:

  1. rescolor 中创建另一个 XML 文件,名称类似于 text_color.xml.

  1. Create another XML file in rescolor named something like text_color.xml.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!-- disabled state -->
  <item android:state_enabled="false" android:color="#9D9FA2" /> 
  <item android:color="#000"/>
</selector>

  • 在您的 style.xml 中,引用该 text_color.xml 文件,如下所示:

  • In your style.xml, put a reference to that text_color.xml file as follows:

    <style name="buttonStyle" parent="@android:style/Widget.Button">
      <item name="android:textStyle">bold</item>
      <item name="android:textColor">@color/text_color</item>
      <item name="android:textSize">18sp</item>
    </style>
    

  • 这应该可以解决您的问题.

    This should resolve your issue.

    这篇关于Android:选择器中禁用按钮的 textColor 未显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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