Android UI自定义形状

1、在res/drawable目录下创建一个shape.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <solid android:color="#ffffffff" />  
  5.   
  6.     <corners android:radius="10dp" />  
  7.   
  8.     <padding  
  9.         android:bottom="10dp"  
  10.         android:left="5dp"  
  11.         android:right="5dp"  
  12.         android:top="5dp" />  
  13.   
  14.     <stroke  
  15.         android:width="2dp"  
  16.         android:color="#ff00bbe8" >  
  17.     </stroke>  
  18.   
  19. </shape>  

2、在需要的地方调用:

[html]  view plain  copy
  1. <EditText  
  2.         android:id="@+id/mText"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="100px"  
  5.         android:background="@drawable/shape"   
  6.         android:textColor="#aa000000"  
  7.         android:textSize="20dp"  
  8.         android:gravity="top"  
  9.         android:layout_margin="3dp"  
  10.         android:paddingLeft="10dp"  
  11.         android:paddingRight="10dp"/>  

3、看下效果图:

4、关于shape的其他属性:

shape与selector综合使用

[html]  view plain  copy
  1. button_selector.xml:  
  2. <?xml version="1.0" encoding="utf-8"?>  
  3. <selector  
  4.     xmlns:android="http://schemas.android.com/apk/res/android">  
  5.     <item android:state_pressed="true" >  
  6.         <shape>  
  7.             <!-- 渐变 -->  
  8.             <gradient  
  9.                 android:startColor="#ff8c00"  
  10.                 android:endColor="#FFFFFF"  
  11.                 android:type="radial"  
  12.                 android:gradientRadius="50" />  
  13.             <!-- 描边 -->  
  14.             <stroke  
  15.                 android:width="2dp"  
  16.                 android:color="#dcdcdc"  
  17.                 android:dashWidth="5dp"  
  18.                 android:dashGap="3dp" />  
  19.             <!-- 圆角 -->  
  20.             <corners  
  21.                 android:radius="2dp" />  
  22.             <padding  
  23.                 android:left="10dp"  
  24.                 android:top="10dp"  
  25.                 android:right="10dp"  
  26.                 android:bottom="10dp" />  
  27.         </shape>  
  28.     </item>  
  29.   
  30.     <item android:state_focused="true" >  
  31.         <shape>  
  32.             <gradient  
  33.                 android:startColor="#ffc2b7"  
  34.                 android:endColor="#ffc2b7"  
  35.                 android:angle="270" />  
  36.             <stroke  
  37.                 android:width="2dp"  
  38.                 android:color="#dcdcdc" />  
  39.             <corners  
  40.                 android:radius="2dp" />  
  41.             <padding  
  42.                 android:left="10dp"  
  43.                 android:top="10dp"  
  44.                 android:right="10dp"  
  45.                 android:bottom="10dp" />  
  46.         </shape>  
  47.     </item>  
  48.   
  49.     <item>        
  50.         <shape>  
  51.             <solid android:color="#ff9d77"/>  
  52.             <stroke  
  53.                 android:width="2dp"  
  54.                 android:color="#fad3cf" />  
  55.             <corners  
  56.                 android:topRightRadius="5dp"  
  57.                 android:bottomLeftRadius="5dp"  
  58.                 android:topLeftRadius="0dp"  
  59.                 android:bottomRightRadius="0dp"  
  60.             />  
  61.             <padding  
  62.                 android:left="10dp"  
  63.                 android:top="10dp"  
  64.                 android:right="10dp"  
  65.                 android:bottom="10dp" />  
  66.         </shape>  
  67.     </item>  
  68. </selector>  

solid:实心,就是填充的意思
android:color 指定填充的颜色

gradient:渐变
android:startColor和android:endColor分别为起始和结束颜色,ndroid:angle是渐变角度, 必须为45的整数倍
另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientRadius="50"。

stroke:描边
android:width="2dp" 描边的宽度,android:color 描边的颜色。
我们还可以把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp" 
android:dashGap="3dp"
其中android:dashWidth表示'-'这样一个横线的宽度,android:dashGap表示之间隔开的距离。

corners:圆角
android:radius为角的弧度,值越大角越圆。
我们还可以把四个角设定成不同的角度,方法为:
<corners 
        android:topRightRadius="20dp"    右上角
        android:bottomLeftRadius="20dp"    右下角
        android:topLeftRadius="1dp"    左上角
        android:bottomRightRadius="0dp"    左下角
 />

padding:间隔

猜你喜欢

转载自blog.csdn.net/qq_35427437/article/details/80053099
今日推荐