Android中的Shape和Selector的結合使用

來源:互聯網
上載者:User

   這篇文章主要介紹了Android中的Shape和Selector的結合使用執行個體,本文直接給出執行個體代碼,需要的朋友可以參考下

  一:在Android程式開發中,我們經常會去用到Shape這個東西去定義各種各樣的形狀,首先我們瞭解一下Shape下面有哪些標籤,都代表什麼意思:

  (1).solid:填充

  android:color指定填充的顏色

  (2).gradient:漸層

  android:startColor和android:endColor分別為起始和結束顏色,

  android:angle是漸層角度,必須為45的整數倍。

  另外漸層預設的模式為android:type=”linear”,即線性漸層,

  可以指定漸層為放射狀漸層,android:type=”radial”,放射狀漸層需要指定半徑android:gradientRadius=”50”。

  angle值對應的位置如圖:

  (3).stroke:描邊

  android:width=”2dp” 描邊的寬度,android:color 描邊的顏色。

  我們還可以把描邊弄成虛線的形式,設定方式為:

  android:dashWidth=”5dp”

  android:dashGap=”3dp”

  其中android:dashWidth表示'-‘這樣一個橫線的寬度,android:dashGap表示之間隔開的距離

  (4).corners:圓角

  android:radius為角的弧度,值越大角越圓。

  我們還可以把四個角設定成不同的角度,同時設定五個屬性,則Radius屬性無效

  android:Radius=”20dp” 設定四個角的半徑

  android:topLeftRadius=”20dp” 設定左上方的半徑

  android:topRightRadius=”20dp” 設定右上方的半徑

  android:bottomLeftRadius=”20dp” 設定右下角的半徑

  android:bottomRightRadius=”20dp” 設定左下角的半徑

  (5).padding:間隔

  可以設定上下左右四個方向的間隔

  二:代碼如下:

  (1).button_bg.xml如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" >   <!-- 漸層 --> <gradient android:endColor="#FFFFFF" android:gradientRadius="50" android:startColor="#ff8c00" android:type="radial" />   <!-- 描邊 --> <stroke android:dashGap="3dp" android:dashWidth="5dp" android:width="2dp" android:color="#dcdcdc" />   <!-- 圓角 --> <corners android:radius="5dp" />   <!-- 間隔 --> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />   </shape>

  (2).shape_image.xml如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" >   <!-- 填充 --> <solid android:color="#ff9d77" /> <!-- 定義填充的顏色值 -->     <!-- 描邊 --> <stroke android:width="2dp" android:color="#fad3cf" /> <!-- 定義描邊的寬度和描邊的顏色值 -->     <!-- 圓角 --> <corners android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" /> <!-- 設定四個角的半徑 -->     <!-- 間隔 --> <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" /> <!-- 設定各個方向的間隔 -->   </shape>

  (3).button.xml如下:

  ?

1 2 3 4 5 6 7 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">   <item android:drawable="@drawable/button_pressed_bg" android:state_pressed="true"></item> <item android:drawable="@drawable/shape_image"></item>   </selector>

  QQ空間 新浪微博 騰訊微博 搜狐微博 人人網 開

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.