【问题标题】:Change the color of a disabled button in android更改android中禁用按钮的颜色
【发布时间】:2015-10-11 23:57:33
【问题描述】:

有没有办法通过样式或其他形式更改android中禁用按钮的颜色?

我目前有以下,

drawable/button_default.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_default_shape"/>
</selector>

drawable/button_default_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="@color/button_default_background"/>
    <corners android:radius="3dp"/>
</shape>

values/styles.xml

<style name="AppTheme.Button">
    <item name="android:background">@drawable/button_default</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:paddingTop">10dp</item>
    <item name="android:paddingBottom">10dp</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
    <item name="android:gravity">center</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">17sp</item>
    <item name="android:textAppearance">@style/CustomFontAppearance</item>
</style>

【问题讨论】:

标签: android button android-drawable


【解决方案1】:

在这些状态下,您必须为不同的可绘制对象使用选择器。

你可以像这样创建一个选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/your_enabled_drawable" android:state_enabled="true" />
    <item android:drawable="@drawable/your_disabled_drawable" android:state_enabled="false" />
    <!-- default state-->
    <item android:drawable="@drawable/your_enabled_drawable" />
</selector>

【讨论】:

  • 非常好的解决方案
【解决方案2】:

在选择器中为 android:state_enabled="false" 指定颜色为可绘制

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false">
        <shape>
            <solid android:color="#32ff09" />
        </shape>
    </item>
</selector>

并将这个可绘制资源应用到按钮的背景

 <Button
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/drawble_name"
    android:enabled="false"
    android:text="Selector Applied Button" />

【讨论】:

    【解决方案3】:

    试试这个 -

    drawable/bg_button.xml :-

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:drawable="@drawable/bg_button_focused" android:state_selected="true"/>
        <item android:drawable="@drawable/bg_button_pressed" android:state_pressed="true"/>
        <item android:drawable="@drawable/bg_button_disabled" android:state_enabled="false" />
        <item android:drawable="@drawable/bg_button_normal"/>
    
    </selector>
    

    在此之后,只需像这样在按钮上设置背景 -

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_button"
        android:text="Button"/>
    

    【讨论】:

      【解决方案4】:

      更新:对于 Android MaterialButton

      对于 Android MaterialButton,有两种解决方案,一种用于 SDK >= v21,另一种用于 SDK

      这里我还给出了如何根据它是启用还是禁用来更改按钮的文本颜色。

      首先在您的color 文件夹中创建两个颜色选择器.xml 文件。一个用于按钮颜色,另一个用于按钮文本颜色。例如,button_state_colors.xml 用于按钮颜色,button_text_state_colors.xml 用于文本颜色。

      这是这两个颜色文件:

      1. button_state_colors.xml:

         <?xml version="1.0" encoding="utf-8"?>
         <selector xmlns:android="http://schemas.android.com/apk/res/android">
             <item android:state_enabled="true" android:color="#303F9F" />
             <item android:state_enabled="false" android:color="#BBBBBB" />
         </selector>
        
      2. button_text_state_colors.xml:

         <?xml version="1.0" encoding="utf-8"?>
         <selector xmlns:android="http://schemas.android.com/apk/res/android">
             <item android:state_enabled="true" android:color="#FFFFFF" />
             <item android:state_enabled="false" android:color="#555555" />
         </selector>
        

      现在对于所有版本的 Android SDK,您都可以使用如下样式更改按钮的颜色和文本:

      style.xml:

      <style name="Widget.AppTheme.MaterialButton" parent="Widget.MaterialComponents.Button">
          <item name="backgroundTint">@color/button_state_colors</item>
          <item name="android:textColor">@color/button_text_state_colors</item>
      </style>
      

      layout.xml:

      <com.google.android.material.button.MaterialButton
          android:id="@+id/button"
          style="@style/Widget.AppTheme.MaterialButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>
      

      仅适用于 SDK >= 21,您可以直接更改按钮颜色,而无需使用 style,例如:

      <com.google.android.material.button.MaterialButton
          android:id="@+id/button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:backgroundTint="@color/button_state_colors"/>
      

      【讨论】:

        【解决方案5】:

        实现此目的的另一种方法是创建一个颜色选择器

        创建文件

        res/color/your_color.xml
        

        看起来像

        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:color="@color/colorDisabledBackground" android:state_enabled="false" />
            <item android:color="@color/colorEnabledBackground" />
        </selector>
        

        那你就可以把它当成普通颜色了

        风格:

        <style name="YourStyle">
             <item name="android:background">@color/your_color</item>
             <item name="android:textColor">@android:color/black</item>
        </style>
        

        在布局、形状或代码等方面也是如此。

        【讨论】:

          【解决方案6】:

          这是我的代码,可以在按钮启用和禁用状态下正常工作。

          <?xml version="1.0" encoding="utf-8"?>
          <selector xmlns:android="http://schemas.android.com/apk/res/android">
             <item android:state_enabled="true" android:drawable="@drawable/ic_button_gradient"/>
             <item android:state_enabled="false" android:drawable="@color/gray"/>
          </selector>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-04-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-04-18
            • 1970-01-01
            • 2013-03-31
            • 2017-11-07
            相关资源
            最近更新 更多