Android-----使用Button特效 selector+shape


當然除了使用drawable這樣的圖片外今天談下自定義圖形shape的方法,對於Button控件Android上支持以下幾種屬性shape、gradient、stroke、corners等。

  我們就以目前系統的Button的selector為例說下:

Java代碼   
  1. <shape>  
  2.             <gradient  
  3.                 android:startColor="#ff8c00"  
  4.                 android:endColor="#FFFFFF"  
  5.                 android:angle="270" />  
  6.             <stroke  
  7.                 android:width="2dp"  
  8.                 android:color="#dcdcdc" />  
  9.             <corners  
  10.                 android:radius="2dp" />  
  11.             <padding  
  12.                 android:left="10dp"  
  13.                 android:top="10dp"  
  14.                 android:right="10dp"  
  15.                 android:bottom="10dp" />  
  16.         </shape>  

 

對於上面,這條shape的定義,分別為漸變,在gradient中startColor屬性為開始的顏色,endColor為漸變結束的顏色,下面的angle是角度。接下來是stroke可以理解為邊緣,corners為拐角這里radius屬性為半徑,最后是相對位置屬性padding。

 對於一個Button完整的定義可以為:

Java代碼   
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector  
  3.     xmlns:android="http://www.norkoo.com">  
  4.     <item android:state_pressed="true" >  
  5.         <shape>  
  6.             <gradient  
  7.                 android:startColor="#ff8c00"  
  8.                 android:endColor="#FFFFFF"  
  9.                 android:angle="270" />  
  10.             <stroke  
  11.                 android:width="2dp"  
  12.                 android:color="#dcdcdc" />  
  13.             <corners  
  14.                 android:radius="2dp" />  
  15.             <padding  
  16.                 android:left="10dp"  
  17.                 android:top="10dp"  
  18.                 android:right="10dp"  
  19.                 android:bottom="10dp" />  
  20.         </shape>  
  21.     </item>  
  22.   
  23.     <item android:state_focused="true" >  
  24.         <shape>  
  25.             <gradient  
  26.                 android:startColor="#ffc2b7"  
  27.                 android:endColor="#ffc2b7"  
  28.                 android:angle="270" />  
  29.             <stroke  
  30.                 android:width="2dp"  
  31.                 android:color="#dcdcdc" />  
  32.             <corners  
  33.                 android:radius="2dp" />  
  34.             <padding  
  35.                 android:left="10dp"  
  36.                 android:top="10dp"  
  37.                 android:right="10dp"  
  38.                 android:bottom="10dp" />  
  39.         </shape>  
  40.     </item>  
  41.   
  42.     <item>          
  43.         <shape>  
  44.             <gradient  
  45.                 android:startColor="#ff9d77"  
  46.                 android:endColor="#ff9d77"  
  47.                 android:angle="270" />  
  48.             <stroke  
  49.                 android:width="2dp"  
  50.                 android:color="#fad3cf" />  
  51.             <corners  
  52.                 android:radius="2dp" />  
  53.             <padding  
  54.                 android:left="10dp"  
  55.                 android:top="10dp"  
  56.                 android:right="10dp"  
  57.                 android:bottom="10dp" />  
  58.         </shape>  
  59.     </item>  
  60. </selector>  

 

注意!提示大家,以上幾個item的區別主要是體現在state_pressed按下或state_focused獲得焦點時,當當來判斷顯示什么類型,而沒有state_xxx屬性的item可以看作是常規狀態下。

 

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

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

        <item

            android:color="hex_color"

            android:state_pressed=["true" | "false"]

            android:state_focused=["true" | "false"]

            android:state_selected=["true" | "false"]

            android:state_active=["true" | "false"]

            android:state_checkable=["true" | "false"]

            android:state_checked=["true" | "false"]

            android:state_enabled=["true" | "false"]

            android:state_window_focused=["true" | "false"] />

    </selector>

 

Elements:

<selector>

必須。必須是根元素。包含一個或多個<item>元素。

              Attributes:

                  xmlns:android

                            String,必須。定義XML的命名空間,必須是

                            “http://schemas.android.com/apk/res/android”.

    <item>

              定義特定狀態的color,通過它的特性指定。必須是<selector>的子元素。

              Attributes:

                  android:color

                            16進制顏色。必須。這個顏色由RGB值指定,可帶Alpha。

                            這個值必須以“#”開頭,后面跟隨Alpha-Red-Green-Blue信息:

l   #RGB

l   #ARGB

l   #RRGGBB

l   #AARRGGBB

    android:state_pressed

Boolean。“true”表示按下狀態使用(例如按鈕按下);“false”表示非按下狀態使用。

                  android:state_focused

Boolean。“true”表示聚焦狀態使用(例如使用滾動球/D-pad聚焦Button);“false”表示非聚焦狀態使用。

                  android:state_selected

Boolean。“true”表示選中狀態使用(例如Tab打開);“false”表示非選中狀態使用。

                  android:state_checkable

Boolean。“true”表示可勾選狀態時使用;“false”表示非可勾選狀態使用。(只對能切換可勾選—非可勾選的構件有用。)

                  android:state_checked

                            Boolean。“true”表示勾選狀態使用;“false”表示非勾選狀態使用。

                  android:state_enabled

Boolean。“true”表示可用狀態使用(能接收觸摸/點擊事件);“false”表示不可用狀態使用。

                  android:window_focused

Boolean。“true”表示應用程序窗口有焦點時使用(應用程序在前台);“false”表示無焦點時使用(例如Notification欄拉下或對話框顯示)。

注意:記住一點,StateList中第一個匹配當前狀態的item會被使用。因此,如果第一個item沒有任何狀態特性的話,那么它將每次都被使用,這也是為什么默認的值必須總是在最后(如下面的例子所示)。

 

Examples:

XML文件保存在res/color/button_text.xml

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

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

    <item android:state_pressed="true"

          android:color="#ffff0000"/> <!-- pressed -->

    <item android:state_focused="true"

          android:color="#ff0000ff"/> <!-- focused -->

    <item android:color="#ff000000"/> <!-- default -->

</selector>

 

    這個Layout XML會應用ColorStateList到一個View上:

<Button

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/button_text"

    android:textColor="@color/button_text" />

 

來源(一):http://www.norkoo.com/show/Mobile_Technology/android/haihkhjijkijajkihgcdkiibkbcee.aspx

來源(二):

http://www.cnblogs.com/xirihanlin/archive/2010/06/11/1756121.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM