Android:选择器中禁用按钮的textColor 未显示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11225166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:24:03  来源:igfitidea点击:

Android: textColor of disabled button in selector not showing?

androidxmlbuttonselectorbackground-image

提问by Nouran H

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

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

  • Enabled/Disabled
  • Pressed/Not Pressed
  • 启用/禁用
  • 按下/未按下

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

根据上述国家。我需要操纵按钮的:

  • Text color
  • background image
  • 文字颜色
  • 背景图片

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!

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

Here is my selector button_selector.xml

这是我的选择器 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>

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

这是我的 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" />

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

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

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

Please help!

请帮忙!

回答by Adil Soomro

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

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

Do the following:

请执行下列操作:

  1. Create another XML file in res\colornamed 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>
    
  2. In your style.xml, put a reference to that text_color.xmlfile 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>
    
  1. 创建另一个res\color名为 .xml 的XML 文件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>
    
  2. 在您的 中style.xmltext_color.xml按如下方式引用该文件:

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

这应该可以解决您的问题。

回答by Bhaskar Kumar Singh

1.Create a color folder in /res/ folder and in color folder create on xml:

1.在 /res/ 文件夹中创建一个颜色文件夹,并在 xml 上创建的颜色文件夹中:

text_color_selector.xml

text_color_selector.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="#776678" /> 
 <item android:color="#ffffff"/>
</selector>

2.Now Create a xml layout:-

2.现在创建一个xml布局:-

 <Button

        android:id="@+id/button_search"

        android:layout_width="652dp"

        android:layout_height="48dp"

        android:layout_alignParentLeft="true"

        android:layout_alignParentTop="true"

        android:layout_marginTop="18dp"

        android:background="@android:color/transparent"

        android:text="Hello Bhaskar"

        android:textColor="@color/text_color_selector"/>  

回答by Woody

The most easy solution is to set color filter to the background image of and button as I saw here

最简单的解决方案是将滤色器设置为和按钮的背景图像,正如我在这里看到的

You can do as follow:

您可以执行以下操作:

if ('need to set button disable')
    button.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
else
    button.getBackground().setColorFilter(null);

Hope I helped someone...

希望我帮助了某人...

回答by Aqif Hamid

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

I cannot see diabling of your button in your layout xml. add this to your button layout.

我在你的布局 xml 中看不到你的按钮被禁用。将此添加到您的按钮布局。

android:enabled="false"

so your Button layout will be,

所以你的按钮布局将是,

<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:enabled="false"
        android:paddingRight="15dp"
        android:layout_gravity="left"
        style="@style/buttonStyle"
        android:background="@drawable/button_selector" />

回答by Lal Krishna

You can create a color list

您可以创建一个颜色列表

file location:

文件位置:

res/color/filename.xml

The filename will be used as the resource ID.

文件名将用作资源 ID。

resource reference:

资源参考:

In Java: R.color.filename

在 Java 中: R.color.filename

In XML: @[package:]color/filename

在 XML 中: @[package:]color/filename

syntax:

句法:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:color="hex_color"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
</selector>

Example:

例子:

XML file saved at res/color/button_text.xml:

XML 文件保存在res/color/button_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#ffff0000"/> <!-- pressed -->
    <item android:state_focused="true"
          android:color="#ff0000ff"/> <!-- focused -->
    <item android:color="#ff000000"/> <!-- default -->
</selector>

This layout XML will apply the color list to a View:

此布局 XML 将颜色列表应用于视图:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:textColor="@color/button_text" />

Refer: Color List Reference

参考:颜色列表参考