Android Selector和Shape

來源:互聯網
上載者:User

 1.      背景選取器(位於res/drawable/,使用方法:android:background=”@drawable/XXX”)

<?xml version="1.0" encoding="utf-8"?>

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

    <itemandroid:drawable="@drawable/login_blue_pressed" android:state_pressed="true"/>

    <itemandroid:drawable="@drawable/login_blue_pressed"android:state_focused="true"/>

    <itemandroid:drawable="@drawable/login_blue_normal" />

</selector>

 

2.      文字顏色選取器(位於res/color/,使用方法:android:textColor=”@color/XXX”)

<?xml version="1.0" encoding="utf-8"?>

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

    <itemandroid:color="#ffffff" android:state_pressed="true"/>

    <itemandroid:color="#ffffff" android:state_focused="true"/>

    <item android:color="#000000"/>

</selector>

注意:在res/drawable/XXX.xml智能提示時沒有android:color,同理在res/color/XXX.xml智能提示時沒有android:drawable。

 

3.背景漸層器(位於res/color/或res/drawable/)

res/drawable/selector1.xml:

<?xml version="1.0" encoding="utf-8"?>

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

    <itemAndroid:state_selected="true">

        <shape>

            <gradientAndroid:angle="270" android:endColor="#99BD4C"

               android:startColor="#A5D245" />

            <sizeAndroid:height="60dp" android:width="320dp" />

            <cornersandroid:radius="8dp" />

        </shape>

    </item>

    <itemAndroid:state_pressed="true">

        <shape>

            <gradientAndroid:angle="270" android:endColor="#99BD4C"

               android:startColor="#A5D245"/>

            <sizeAndroid:height="60dp" android:width="320dp" />

            <cornersandroid:radius="8dp" />

        </shape>

    </item>

    <item>

        <shape>

            <gradientAndroid:angle="270" android:endColor="#A8C3B0"

                android:startColor="#C6CFCE"  />

            <sizeAndroid:height="60dp" android:width="320dp" />

            <cornersandroid:radius="8dp" />

        </shape>

    </item>

</selector>

layout/main.xml:

<Button

   android:id="@+id/button1"

    android:layout_width="100dp"

    android:layout_height="50dp"

   android:background="@drawable/selector1"

   android:text="button1" />

結合Shape來實現背景漸層效果。

或者只是以color代替drawable作為背景選取器的元素:

<?xml version="1.0" encoding="utf-8"?>

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

    <itemAndroid:state_selected="true">

        <shape>

            <solidAndroid:color="#ff0000" />

        </shape>

    </item>

    <itemAndroid:state_pressed="true">

        <shape>

            <solidAndroid:color="#ff0000" />

        </shape>

    </item>

    <item>

        <shape>

            <solidAndroid:color="#00ffff" />

        </shape>

    </item>

</selector>

再或者drawable和color混用(不過注意四角的一致性,因為drawable裡可能圓弧型四角):

<?xml version="1.0" encoding="utf-8"?>

<selectorxmlns:Android="http://schemas.android.com/apk/res/android">

    <itemAndroid:state_selected="true">

        <shape>

            <solidAndroid:color="#ff0000" />

        </shape>

    </item>

    <itemAndroid:state_pressed="true">

        <shape>

            <solidAndroid:color="#ff0000" />

        </shape>

    </item>

    <itemAndroid:drawable="@drawable/login_blue_normal">

    </item>

</selector>

 


 

 

 
 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.