Android實現圓角邊框

設置邊框圓角能夠在drawable-mdpi目錄裏定義一個xml: android

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

  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">    spa

  3.     <solid android:color="#000000" />    orm

  4.     <corners android:topLeftRadius="10dp"   xml

  5.                     android:topRightRadius="10dp"    ip

  6.                 android:bottomRightRadius="10dp"   utf-8

  7.                 android:bottomLeftRadius="10dp"/>    string

  8. </shape>  it


解釋:solid的表示填充顏色,爲了簡單,這裏用的是黑色。 
而corners則是表示圓角,注意的是這裏bottomRightRadius是左下角而不是右下角,bottomLeftRadius右下角。 
固然上面的效果也能夠像下面同樣設置,以下: io

  1. <corners android:radius="5dp" />  


若是想引用這個xml,只須要@drawable/corners_bg.xml便可: 

  1. android:background="@drawable/corners_bg"  



main.xml: 

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

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  3.     android:orientation="vertical" android:layout_width="fill_parent"  

  4.     android:layout_height="fill_parent" android:background="#FFFFFF">  

  5.     <RelativeLayout android:id="@+id/login_div"  

  6.         android:layout_width="fill_parent" android:layout_height="150dip"  

  7.         android:padding="15dip" android:layout_margin="15dip"  

  8.         android:background="@drawable/corners_bg">  

  9.     </RelativeLayout>  

  10. </LinearLayout>  



運行結果: