简体   繁体   English

Android使用图层列表作为按钮选择器

[英]Android Using layer-list for button selector

How do we use a layer-list as a drawable for a button. 我们如何使用图层列表作为按钮的drawable。 I have a button: 我有一个按钮:

<item android:state_pressed="true">
    <shape>
        <gradient android:endColor="@color/white"
            android:startColor="@color/grey_blue_light" android:angle="90" />
        <stroke android:width="1dp" android:color="@color/aqua_blue" />
        <corners android:radius="3dp" />
        <padding android:left="10dp" android:top="10dp"
            android:right="10dp" android:bottom="10dp" />
    </shape>
</item>


<item android:state_focused="true">

</item>
<item>

</item>

Now I need a layer-list to be used as a shape when say button state is pressed: 现在,当按下按钮状态时,我需要一个图层列表用作形状:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="oval">
        <solid android:color="@color/aqua_blue" />
    </shape>
</item>
<item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="1dp">
    <shape android:shape="oval">
        <solid android:color="@color/aqua_blue" />
    </shape>
</item>

How do we use this layer list in the button selector? 我们如何在按钮选择器中使用此图层列表?

Step-1 create three different layer_list xml under drawable folder for three different state of button. 步骤1在drawable文件夹下为三个不同的按钮状态创建三个不同的layer_list xml。 example the name of those xml is layer1.xml, layer2.xml, layer3.xml 例如,那些xml的名称是layer1.xml, layer2.xml, layer3.xml

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

            <gradient
                android:angle="270"
                android:startColor="#0000ff"
                android:endColor="#0000dd"
                android:type="linear"
                />    
        </shape>
    </item>

</layer-list>

Step-2 create a selector xml named as btn_background.xml and pass the layer_list xml in drawable attribute Step-2创建一个名为btn_background.xml的选择器xml,并在drawable属性中传递layer_list xml

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

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

    </item>

    <item android:state_focused="true" android:drawable="@drawable/layer2">

    </item>

    <item android:drawable="@drawable/layer3">        

    </item>
</selector>

step-3 Set the selector xml as background of the button android:background="@drawable/btn_background" 步骤3将选择器xml设置为按钮android:background="@drawable/btn_background"

Mygod's answer is actually better than the accepted one. Mygod的回答实际上比接受的回答要好。 It works and only needs one xml file. 它工作,只需要一个xml文件。 Here's something similar that I did. 这与我所做的类似。 It sets the background under a drawable with transparencies. 它使用透明胶片将背景设置为可绘制的。 The same could be done with a background and a shape: 使用背景和形状也可以这样做:

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

    <item android:state_selected="true">
        <layer-list>
            <item android:drawable="@color/Black"/>
            <item android:drawable="@drawable/notes_white"/>
        </layer-list>
    </item>

    <item>
        <layer-list>
            <item android:drawable="@color/White"/>
            <item android:drawable="@drawable/notes_black"/>
        </layer-list>
    </item>

</selector>

只需用layer-list替换shape标签,一切都会正常工作。

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

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