简体   繁体   English

Android:使用选择器更改按钮背景图像和颜色

[英]Android : Using selector to change button background image and color

I have a button I set its background to specific selector . 我有一个按钮,我将其背景设置为特定的selector
The selector currently changes the button background and changed an image as the background. 选择器当前更改按钮背景并将图像更改为背景。
I also want the background color to be changed (the image is icon with transparent space around). 我还想要更改背景颜色(图像是带有透明空间的图标)。
This is the selector : 这是选择器:

    <?xml version="1.0" encoding="utf-8"?>
        <selector 
            xmlns:android="http://schemas.android.com/apk/res/android" >
        <!-- default -->
        <item 
            android:state_pressed="false" 
            android:state_focused="false"
            android:drawable="@drawable/menu_button_collapsed" >
        </item>

        <!-- button focused -->
        <item 
            android:state_pressed="false" 
            android:state_focused="true"
            android:drawable="@drawable/menu_button_collapsed_highlight" 
            android:drawable="@drawable/button_background" >
        </item>

       <!-- button pressed -->
        <item 
            android:state_pressed="true" 
            android:state_focused="false"
            android:drawable="@drawable/menu_button_collapsed_highlight"
            android:drawable="@drawable/button_background" >
        </item>
    </selector>

As you can see, I set the drawable attribute twice, which is illegal, but this is what I want actually. 如您所见,我将drawable属性设置了两次,这是非法的,但这实际上是我想要的。
Notice @drawable/button_background is just a color 注意@drawable/button_background只是一种颜色

Create a new <layer-list> drawable 创建一个新的<layer-list> drawable

custom_button.xml custom_button.xml

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

           <!-- Your background color goes first -->
           <item 
             android:id="@android:id/background"
             android:drawable="@drawable/button_background" />

           <!-- Your button icon image -->
           <item 
             android:id="@android:id/button_image"
             android:drawable="@drawable/menu_button_collapsed_highlight" />

        </layer-list>

And reference it in your selector drawable file 并在您的选择器可绘制文件中引用它

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

        <!-- default -->
        <item 
            android:state_pressed="false" 
            android:state_focused="false"
            android:drawable="@drawable/menu_button_collapsed" 
         />            

        <!-- button focused -->
        <item 
            android:state_pressed="false" 
            android:state_focused="true"
            android:drawable="@drawable/custom_button" 
            />

        <!-- button pressed -->
        <item 
            android:state_pressed="true" 
            android:state_focused="false"
            android:drawable="@drawable/custom_button" 
         />
    </selector>

<!-- default -->
<item
    android:state_pressed="false"
    android:state_focused="false">
    <shape
        android:innerRadiusRatio="1"
        android:shape="rectangle" >
        <solid android:color="#01AF7E" />
        <corners
            android:bottomLeftRadius="5dp"
            android:bottomRightRadius="5dp"
            android:radius="5dp"
            android:topLeftRadius="5dp"
            android:topRightRadius="5dp"></corners>
        <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />

    </shape></item>

<!-- button focused -->
<item
    android:state_pressed="false"
    android:state_focused="true">
    <shape
        android:innerRadiusRatio="1"
        android:shape="rectangle" >
        <solid android:color="#8001AF7E" />
        <corners
            android:bottomLeftRadius="5dp"
            android:bottomRightRadius="5dp"
            android:radius="5dp"
            android:topLeftRadius="5dp"
            android:topRightRadius="5dp"></corners>
        <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />

    </shape></item>

<!-- button pressed -->
<item
    android:state_pressed="true"
    android:state_focused="false">
    <shape
        android:innerRadiusRatio="1"
        android:shape="rectangle" >
        <solid android:color="#8001AF7E" />
        <corners
            android:bottomLeftRadius="5dp"
            android:bottomRightRadius="5dp"
            android:radius="5dp"
            android:topLeftRadius="5dp"
            android:topRightRadius="5dp"></corners>
        <padding
            android:bottom="10dp"
            android:left="10dp"
            android:right="10dp"
            android:top="10dp" />

    </shape></item>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM