Change the color of a disabled button in android

两盒软妹~` 提交于 2020-01-22 10:41:17

问题


Is there a way to change the color of a disabled button in android through styles or some other form ?

I currently have the following,

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>

回答1:


You'll have to use a selector for different drawables in those states.

You can create a selector like this:

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


Try this -

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>

After this just set background on your button like this -

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



回答3:


Specify the color in selector for android:state_enabled="false" as drawable

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

And apply this drawable resource to background of button

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



回答4:


Another way to achieve this is to create a color selector.

Create a file

res/color/your_color.xml

which looks like

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

Then you may use it as a regular color

in a style:

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

As well is in layouts, shapes or in code as etc.




回答5:


Here is my code which works properly with button enable and disable state.

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


来源:https://stackoverflow.com/questions/33071410/change-the-color-of-a-disabled-button-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!