android中通过自定义xml实现你需要的shape效果 xml属性配置
2016-12-23 09:53
676 查看
转载地址:http://www.xuebuyuan.com/1156365.html
在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决。不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片,然后在写一个selector的xml文件放在drawable文件夹中,但是 这样drawable中的内容就非常多,就容易使apk变大。
那么除了使用drawable这样的图片外,还有其他方法吗?
就谈一下自定义图形shape,Android上支持以下几种属性gradient、stroke、corners、padding、solid等。
1.<shape>
2. <!-- 实心 -->
3. <solid android:color="#ff9d77"/>
4. <!-- 渐变 -->
5. <gradient
6. android:startColor="#ff8c00"
7. android:endColor="#FFFFFF"
8. android:angle="270" />
9. <!-- 描边 -->
10. <stroke
11. android:width="2dp"
12. android:color="#dcdcdc" />
13. <!-- 圆角 -->
14. <corners
15. android:radius="2dp" />
16. <padding
17. android:left="10dp"
18. android:top="10dp"
19. android:right="10dp"
20. android:bottom="10dp" />
21.</shape>
solid:实心,就是填充的意思
android:color指定填充的颜色
gradient:渐变
android:startColor和android:endColor分别为起始和结束颜色
android: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为角的弧度,值越大角越圆。
在Android开发过程中,经常需要改变控件的默认样式, 那么通常会使用多个图片来解决。不过这种方式可能需要多个图片,比如一个按钮,需要点击时的式样图片,默认的式样图片,然后在写一个selector的xml文件放在drawable文件夹中,但是 这样drawable中的内容就非常多,就容易使apk变大。
那么除了使用drawable这样的图片外,还有其他方法吗?
就谈一下自定义图形shape,Android上支持以下几种属性gradient、stroke、corners、padding、solid等。
1.<shape>
2. <!-- 实心 -->
3. <solid android:color="#ff9d77"/>
4. <!-- 渐变 -->
5. <gradient
6. android:startColor="#ff8c00"
7. android:endColor="#FFFFFF"
8. android:angle="270" />
9. <!-- 描边 -->
10. <stroke
11. android:width="2dp"
12. android:color="#dcdcdc" />
13. <!-- 圆角 -->
14. <corners
15. android:radius="2dp" />
16. <padding
17. android:left="10dp"
18. android:top="10dp"
19. android:right="10dp"
20. android:bottom="10dp" />
21.</shape>
solid:实心,就是填充的意思
android:color指定填充的颜色
gradient:渐变
android:startColor和android:endColor分别为起始和结束颜色
android: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为角的弧度,值越大角越圆。
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/y_calendar_btn_delete1" android:state_pressed="false"/> <it 4000 em android:drawable="@drawable/y_calendar_btn_delete2" android:state_pressed="true"/> </selector> <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <!-- <stroke android:width="1sp" android:color="#c9c9c9" /> 边框颜色--> <solid android:color="#bac4c5" /><!--填充色--> <corners android:radius="5dip" /> <!--圆角弧度--> </shape> </item> </layer-list> <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <!-- <stroke android:width="1sp" android:color="#c9c9c9" /> 边框颜色--> <solid android:color="#bec9c9" /><!--填充色--> <corners android:radius="5dip" /> <!--圆角弧度--> </shape> </item> </layer-list>
相关文章推荐
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- android中通过自定义xml实现你需要的shape效果 xml属性配置
- android中通过自定义xml实现shape效果 的属性配置
- 为Android自定义部件(widget)实现自定义的XML配置属性
- android开发游记:通过xml配置快速实现点击热点效果和渐变动画(不用java代码)
- 为Android自定义部件(widget)实现自定义的XML配置属性
- TabActivity可以显示实现多页显示效果,在xml布局文件中需要有一些特殊的配置属性
- Android解析自定义属性的XML实现底部导航栏TabSelectedView,实现灵活的配置扩展
- Android自定义一个时间轴,通过ListView来实现时间轴的效果
- 收集Android里xml配置属性在java代码里的实现
- Android 自定义view和属性动画实现充电进度条效果
- android通过shape实现虚线效果
- 【Android】安卓开发实战之通过visibility属性实现搜索框弹出效果
- Android之用自定义的shape去实现shadow效果
- Android UI设计之<四>自定义TextView属性,实现带边框效果的TextView
- 【我的问题】android通过XML实现按钮被按下的反馈效果
- Android编程使用自定义shape实现shadow阴影效果的方法