欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)手冊(cè)Button實(shí)現(xiàn)selector選擇器

 更新時(shí)間:2022年06月11日 09:03:20   作者:芝麻粒兒  
這篇文章主要為大家介紹了Android開發(fā)手冊(cè)Button實(shí)現(xiàn)selector選擇器,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

selector是按鈕最常用的功能,對(duì)美化控件的作用很大。

上節(jié)我們說(shuō)了selector和shape聯(lián)合使用,但偏向shape的介紹,今天主要說(shuō)selector。

??實(shí)踐過(guò)程

我們先按照上一節(jié)的shape方式創(chuàng)建兩個(gè)shape背景 

btn_selector_shape1.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 圓角 -->
    <corners android:radius="5dp" />
    <!--填充顏色-->
    <solid android:color="#00ff00" />
</shape>

btn_selector_shape2.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--圓角-->
    <corners android:radius="5dp" />
    <!--填充顏色-->
    <solid android:color="#0000ff" />
</shape>

接著我們?cè)凇緍es-drawable】右鍵創(chuàng)建個(gè)Drawable Resource File ,彈出框?qū)懳募麆?chuàng)建文件,設(shè)置默認(rèn)【Root element】為selector。

btn_selector0.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_selector_shape1" android:state_pressed="true" />
    <item android:drawable="@drawable/btn_selector_shape2" android:state_window_focused="false" />
</selector>

布局中引用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".TextActivity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="160dp"
        android:background="@drawable/btn_selector0"
        android:text="按下變色"
        android:textColor="@color/white" />
</RelativeLayout>

我們運(yùn)行下看看

但是

我們回憶下,剛才是不是創(chuàng)建了三個(gè)文件,按鈕少的情況下還好,自定義的按鈕一多,這么多文件非常不容易管理,所以我們要用另外一種寫法,將所有內(nèi)容放到一個(gè)文件中。

我們?cè)趧偛诺腷tn.selector0.xml中修改:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--這是第一種方式,利用drwable引用文件-->
    <!--<item android:drawable="@drawable/btn_selector_shape1" android:state_pressed="true" />-->
    <!--<item android:drawable="@drawable/btn_selector_shape2" android:state_pressed="false" />-->
    <!--第二種方式如下-->
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <!-- 圓角 -->
            <corners android:radius="5dp" />
            <!--填充顏色為白色-->
            <solid android:color="#0000ff" />
        </shape>
    </item>
    <!--單擊時(shí)是一個(gè)帶圓角,白色背景,綠色邊框的矩形-->
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <!--圓角-->
            <corners android:radius="5dp" />
            <!--填充顏色為白色-->
            <solid android:color="#00ff00" />
        </shape>
    </item>
</selector>

我們運(yùn)行起來(lái)看看,哎,效果很正確啊

Selector的屬性不止這兩個(gè)哦:

  • state_focused 布爾值,是否獲得焦點(diǎn)
  • state_window_focused 布爾值,是否獲得窗口焦點(diǎn)
  • state_enabled 布爾值,控件是否可用
  • state_checkable 布爾值,控件可否被勾選
  • state_checked 布爾值,控件是否被勾選
  • state_selected 布爾值,控件是否被選擇,針對(duì)有滾輪的情況
  • state_pressed 布爾值,控件是否被按下
  • state_active 布爾值,控件是否處于活動(dòng)狀態(tài)
  • state_single和state_first和state_middle很少使用,知道就行

以上就是Android開發(fā)手冊(cè)Button實(shí)現(xiàn)selector選擇器的詳細(xì)內(nèi)容,更多關(guān)于Android開發(fā)Button selector選擇器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論