自定义背景文件,android:shape的使用!

来源:互联网 发布:a标签用js去掉下划线 编辑:程序博客网 时间:2024/05/16 17:12

Android中使用shape来定义控件的一些简单显示属性,如按钮的背景等,应用灵活方便。

参考代码:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_pressed="true" >  
  4.         <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  5.             android:shape="rectangle">  
  6.             <!-- 渐变 -->   
  7.             <gradient    
  8.                 android:startColor="#55B4FE"    
  9.                 android:endColor="#3d8FFB"    
  10.                 android:angle="-90"  
  11.                 android:type="linear"/>    
  12.             <!-- 描边 -->    
  13.             <stroke android:width="1dip"  
  14.                 android:color="#d3d3d3"  
  15.                 />  
  16.             <!-- 圆角 -->    
  17.             <corners  
  18.                 android:topRightRadius="0dp"       
  19.                 android:bottomLeftRadius="10dp"     
  20.                 android:topLeftRadius="0dp"      
  21.                 android:bottomRightRadius="10dp"      
  22.                 />  
  23.               
  24.         </shape>  
  25.           
  26.           
  27.     </item>  
  28.     <item>  
  29.         <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  30.             android:shape="rectangle">  
  31.               
  32.             <solid android:color="#ffffff" />            
  33.             <!-- 描边 -->    
  34.             <stroke android:width="1dip"  
  35.                 android:color="#d3d3d3"  
  36.                 />  
  37.             <!-- 圆角 -->    
  38.             <corners  
  39.                 android:topRightRadius="0dp"       
  40.                 android:bottomLeftRadius="10dp"     
  41.                 android:topLeftRadius="0dp"      
  42.                 android:bottomRightRadius="10dp"      
  43.                 />  
  44.               
  45.               
  46.         </shape>  
  47.     </item>  
  48. </selector>  

说明:

gradient:渐变

android:startColor 渐变开始的颜色
android:endColor 渐变结束的颜色
android:centerColor 中间点的颜色
ndroid:angle是渐变角度,必须为45的整数倍。
android:type  linear线性渐变;radial径向渐变
android:gradientRadius 径向渐变的半径

solid:填充
android:color 使用的填充颜色

stroke:描边
android:width 描边的宽度,
android:color 描边的颜色。

我们还可以把描边弄成虚线的形式,设置方式为:
android:dashWidth="5dp" 一个'-'的宽度
android:dashGap="3dp" 间隔的宽度


corners:圆角
android:radius为角的弧度,值越大角越圆。

分开设置:
android:topRightRadius="20dp"    右上角  
android:bottomLeftRadius="20dp"    右下角  
android:topLeftRadius="1dp"    左上角  
android:bottomRightRadius="0dp"    左下角  

padding:内间隔

将代码保存在res/drawable目录中,在使用时直接引入文件即可,如:


[html] view plaincopy
  1. <Button   
  2.     android:id="@+id/testButton"   
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="@drawable/test_button_bg"/>  
  6. ……  
0 0
原创粉丝点击