Android開發之狀態選擇器(selector)詳解

這些天博主的項目進度比較快,現在服務器那端進度較慢,所以博主晚上最近會比較有時間,所以最近會多寫些博客出來和大家分享~希望能夠幫助到大家。

所謂狀態選擇器,就是控件(view或者viewgroup)的狀態發現變化的時候,我們可以再指定的狀態下,切換控件的背景屬性(background),從而達到效果絢麗的目的。

先隨便看一個例子:

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

    <item android:drawable="@drawable/btn_langage_content" android:state_focused="true"></item>
    <item android:drawable="@drawable/btn_langage_content" android:state_pressed="true"></item>
    <item android:drawable="@drawable/btn_langage_content" android:state_selected="true"></item>
    <item android:drawable="@drawable/btn_new_message"></item>

</selector>

可以看到狀態選擇器的寫法一般爲一個drawble屬性加一個狀態屬性,當然,狀態屬性是可以寫成多個的。那麼,我們先來講講有哪些常用的狀態屬性。

1、android:state_focused   官方解釋爲當前view獲得焦點的時候

2、android:state_window_focused  當前view所在的窗體獲得焦點的時候

3、android:state_enabled 當前view可以被點擊或者觸發觸摸事件的時候

4、android:state_checkable  當前view是否可以被check,比如checkBox,RadioButton是可以check的

5、android:state_checked   當前view是否被check  比如checkBox被check的

6、android:state_selected  當前view是否被選中

7、android:state_pressed 當前view是否被按下

8、android:state_activated 這個比較難解釋,官方解釋是set when a view or its parent has been "activated" meaning the user has currently marked it as being of interest.

9,什麼都不寫,就是正常狀態下

在講解完狀態屬性之後,我們就可以根據實際需要來配置狀態選擇器了。比如一個button,我們可以設定它被按下時的顏色。比如一個listView,我們可以設定選中某一個item時,item的背景顏色。只要在對應的狀態值下,傳入對應的drawble資源即可。


關於drawble資源,纔是狀態選擇器的重點。首先,drawble資源我們可以用.9patch圖片來替代,但是還有時,是需要我們 自己去寫背景的drawble資源的。那麼,如何自定義一個drawble資源呢?下面來講講android下shape的使用。

先來看例子:

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

    <!-- 圓角 -->
    <corners
        android:radius="10dp"
        android:topLeftRadius="" />
    <!-- 描邊 -->
    <stroke
        android:dashGap=""
        android:dashWidth=""
        android:width="2dp"
        android:color="@color/darkgray" />
    <!-- 實心 -->
    <solid android:color="#c4c4c4" />

    <!-- 大小 -->
    <size
        android:height=""
        android:width="" />
    <!-- 顏色漸變 -->

    <gradient
        android:angle=""
        android:centerColor=""
        android:endColor=""
        android:gradientRadius=""
        android:startColor=""
        android:type="" />

</shape>

首先是最開始的shape標籤。在shape裏面有個shape屬性,這個屬性可以設定,也可以不設定,不設定的時候默認是矩形。設定有四個值可以設定:

1、rectangle 矩形

2、oval 橢圓形  當寬高設定爲相同的時候,就是圓

3、line 線性形狀

4、ring 環形  可用作數據刷新時轉圈的提示

當設定爲ring環形的時候,還需要設定一下幾個屬性

 android:innerRadiusRatio="3"  浮點型數據,以環的寬度比率來表示內環的半徑
 android:thicknessRatio="8"    浮點型數據,以環的寬度比率來表示環的厚度
 android:useLevel="false"  如果當做是LevlListDrawable使用時爲true,其他爲false


接下來定義在shape標籤裏面的節點

 <!-- 圓角 -->
    <corners
        android:radius="10dp"
        android:topLeftRadius="" />
這個表示圓角,可以一次性設定四個邊角的大小。也分個設定四個角度的大小,這裏只寫了全部的和左上角的。


 <!-- 描邊 -->
    <stroke
        android:dashGap=""
        android:dashWidth=""
        android:width="2dp"
        android:color="@color/darkgray" />
這個表示描邊,在邊界畫線。width表示線的厚度,color表示顏色,dashWidth和dashGap是用來畫虛線的時候用的,dashWidth表示虛線的寬度,dashGap表示虛線的間隔。


  <!-- 實心 -->
    <solid android:color="#c4c4c4" />
這個沒什麼好說的。


   <!-- 大小 -->
    <size
        android:height=""
        android:width="" />


   <!-- 顏色漸變 -->

    <gradient
        android:angle=""
        android:centerColor=""
        android:endColor=""
        android:gradientRadius=""
        android:startColor=""
        android:centerX=""
        android:centerY=""
        android:gradientRadius=""
        android:type="" />
顏色漸變需要好好講解一下。

angle表示顏色漸變的起始位置,0表示從左向右然後逆時針方向,90表示從上到下,以此類推,angle必須爲45點整數倍

startColor  endColor  centerColor,顏色 漸變 過程的顏色值。

type,顏色漸變類型,有三個值

1、linear,線性漸變,這個是默認值

2、radial,放射性漸變,這個要配合android:gradientRadius屬性使用,android:gradientRadius表示放射漸變的半徑大小。

3、sweep,掃描石漸變,就像雷達掃描的那個效果。

centerX,centerY,表示漸變中心的X和Y點的座標的相對位置。


這裏博主就不上傳效果圖了,希望這些能夠幫助到大家。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章