Android中shape的基本使用

来源:互联网 发布:js让隐藏的div显示 编辑:程序博客网 时间:2024/06/07 15:52

shape用于设定形状,可以在selector,layout等里面使用,点击效果神马的我们可以不需要UI的切图,自己直接使用shape搞定,比较方便快捷。 新建一个shape.xml默认为矩形,可以通过android:shape=""来设置具体的形状,有这么四种:rectangle 矩形,oval 椭圆形,line 线,ring 环形。shape有6个子标签,各属性如下:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle" >    <!-- 圆角 -->    <corners        android:bottomLeftRadius="8dip"        android:bottomRightRadius="8dip"        android:radius="5dip"        android:topLeftRadius="8dip"        android:topRightRadius="8dip" />    <!-- 颜色渐变 -->    <gradient        android:angle="45"        android:centerColor="@android:color/black"        android:endColor="@android:color/black"        android:startColor="@android:color/white"        android:type="linear" />    <!-- 尺寸大小 -->    <size        android:height="28dip"        android:width="48dip" />    <!-- 描边 -->    <stroke        android:dashGap="2dp"        android:dashWidth="1dp"        android:width="1dip"        android:color="@color/red" />    <!-- 边距 -->    <padding        android:bottom="5dip"        android:left="5dip"        android:right="5dip"        android:top="3dip" />    <!-- 填充 -->    <solid android:color="@color/slateblue" /></shape>
填充:设置填充的颜色

间隔:设置四个方向上的间隔

大小:设置大小

圆角:同时设置五个属性,则Radius属性无效

android:Radius="20dp"                           设置四个角的半径

android:topLeftRadius="20dp"              设置左上角的半径 
android:topRightRadius="20dp"           设置右上角的半径 
android:bottomLeftRadius="20dp"      设置右下角的半径 
android:bottomRightRadius="20dp"    设置左下角的半径

描边:dashWidth和dashGap属性,只要其中一个设置为0dp,则边框为实现边框

android:width="20dp"                               设置边边的宽度 
android:color="@android:color/black"  设置边边的颜色 
android:dashWidth="2dp"                         设置虚线的宽度 
android:dashGap="20dp"                          设置虚线的间隔宽度

渐变:当设置填充颜色后,无渐变效果。angle的值必须是45的倍数(包括0),仅在type="linear"有效,不然会报错。android:useLevel 这个属性不知道有什么用。

0 0