Android自定义ListView实现圆角
Android自定义ListView实现圆角
在项目中我们会经常遇到这种圆角效果,因为直角的看起来确实不那么雅观,可能大家会想到用图片实现,试想上中下要分别做三张图片,这样既会是自己的项目增大也会增加内存使用量,所以使用shape来实现不失为一种更好的实现方式。在这里先看一下shape的使用:
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:Android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle"
- >
- <!-- 渐变 -->
- <gradient
- android:startColor="#B5E7B8"
- android:endColor="#76D37B"
- android:angle="270"
- />
- <!-- 描边 -->
- <!-- <stroke android:width="1dip"
- android:color="@color/blue"
- /> -->
- <!-- 实心 -->
- <!-- <solid android:color="#FFeaeaea"
- /> -->
- <!-- 圆角 -->
- <corners
- android:bottomRightRadius="4dip"
- android:bottomLeftRadius="4dip"
- android:topLeftRadius="4dip"
- android:topRightRadius="4dip"
- />
- </shape>
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为角的弧度,值越大角越圆。
OK,下面开始自定义listview实现圆角的例子:
首先在drawable下定义只有一项的选择器app_list_corner_round.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle"
- >
- <!-- 渐变 -->
- <gradient
- android:startColor="#B5E7B8"
- android:endColor="#76D37B"
- android:angle="270"
- />
- <!-- 描边 -->
- <!-- <stroke android:width="1dip"
- android:color="@color/blue"
- /> -->
- <!-- 实心 -->
- <!-- <solid android:color="#FFeaeaea"
- /> -->
- <!-- 圆角 -->
- <corners
- android:bottomRightRadius="4dip"
- android:bottomLeftRadius="4dip"
- android:topLeftRadius="4dip"
- android:topRightRadius="4dip"
- />
- </shape>
|
评论暂时关闭