Android中的Shape使用總結

來源:互聯網
上載者:User

標籤:

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

solid:填充
android:color指定填充的顏色

 

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

android:angle是漸層角度,必須為45的整數倍。
另外漸層預設的模式為android:type="linear",即線性漸層,

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

angle值對應的位置

 

 

stroke:描邊
android:width="2dp" 描邊的寬度,android:color 描邊的顏色。
我們還可以把描邊弄成虛線的形式,設定方式為:
android:dashWidth="5dp" 
android:dashGap="3dp"
其中android:dashWidth表示‘-‘這樣一個橫線的寬度,android:dashGap表示之間隔開的距離

 

corners:圓角
android:radius為角的弧度,值越大角越圓。
我們還可以把四個角設定成不同的角度,

同時設定五個屬性,則Radius屬性無效

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

android:topLeftRadius="20dp"              設定左上方的半徑 
android:topRightRadius="20dp"           設定右上方的半徑 
android:bottomLeftRadius="20dp"      設定右下角的半徑 
android:bottomRightRadius="20dp"    設定左下角的半徑

 

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

 

在這裡我們來看一個簡單的小例子,ShapDemo,在drawable檔案夾下面先定義兩個xml檔案:

button_bg.xml的內容如下:

 

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


button_pressed_bg.xml的內容如下:

 

 

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


這裡說明一點,在描邊裡面設定了dash參數,使得圖形的邊變成了虛線

 

在drawable檔案夾下添加一個button.xml檔案,內容如下:

 

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


這個檔案的意思以前講過,normal(正常)情況下就顯示button_bg,被press(按下)情況下就顯示button_pressed_bg。

 

 

我們再來看一下layout目錄下的activity_main.xml的內容:

 

[html] view plaincopy 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <Button  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:background="@drawable/button"  
  10.         android:text="TestShapeButton" />  
  11.   
  12. </RelativeLayout>  


直接將background指定為drawable檔案夾下的button.xml。

 

 

程式運行如下:

Android中的Shape使用總結

聯繫我們

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