android使用xml实现一些常用的背景图

来源:互联网 发布:庞培法布拉大学知乎 编辑:程序博客网 时间:2024/05/17 18:44

有图有真相,先上图



第一部分使用shape实现

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <!--    渐变颜色 :     android:angle渐变方向角度    android:endColor结束角度    android:startColor起始角度    -->    <gradient        android:angle="90"        android:endColor="#FFFFFF"        android:startColor="#000000" />    <!-- 圆角半径 -->    <corners        android:bottomLeftRadius="12dp"        android:bottomRightRadius="12dp"        android:topLeftRadius="12dp"        android:topRightRadius="12dp" />    <!--    1、虚线        android:dashWidth="5dp"               android:dashGap="3dp"             去掉这两句就会变成实线    -->    <stroke        android:dashGap="3dp"        android:dashWidth="5dp"        android:width="2dip"        android:color="#000000" />    <!-- 与中心距离 -->    <padding        android:bottom="12dp"        android:left="12dp"        android:right="12dp"        android:top="12dp" /></shape>


有了样式后,再加上点击事件

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true"><shape>            <!-- 渐变 -->            <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="2dp" />            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />        </shape></item>    <item android:state_focused="true"><shape>            <gradient android:angle="270" android:endColor="#ffc2b7" android:startColor="#ffc2b7" />            <stroke android:width="2dp" android:color="#dcdcdc" />            <corners android:radius="2dp" />            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />        </shape></item>    <item><shape>            <solid android:color="#ff9d77" />            <stroke android:width="2dp" android:color="#fad3cf" />            <corners android:bottomLeftRadius="15dp" android:bottomRightRadius="0dp" android:topLeftRadius="0dp" android:topRightRadius="15dp" />            <padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />        </shape></item></selector>


有些情况会用到只需要某几条边有线条

<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 边框框颜色值 -->    <item>        <shape>            <solid android:color="#000000" />        </shape>    </item>    <!-- 主体背景颜色值 -->    <item        android:bottom="1dp"        android:top="1dp">        <shape>            <solid android:color="#ffffff" />        </shape>    </item></layer-list>



附件下载地址

http://download.csdn.net/detail/taven/8409591


欢迎加入Android交流群:31253751 







0 0
原创粉丝点击