android shape的使用详解
2016-05-30 20:53
387 查看
shape的属性大全:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] > --- 默认为rectangle
<corners -- shape=“rectangle”时使用,
android:radius="integer" -- 半径,会被下边的属性覆盖,默认为1dp,
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient -- 渐变
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size -- 指定大小,一般用在imageview配合scaleType属性使用。大小一般会适配滴
android:width="integer"
android:height="integer" />
<solid -- 填充颜色,可是是十六进制颜色。(比如想设置半透明效果,直接使用十六就只就OK)
android:color="color" />
<stroke -- 指定边框,border,dashWidth和dashGap有一个为0dp则为实线
android:width="integer"
android:color="color"
android:dashWidth="integer" -- 虚线宽度
android:dashGap="integer" /> -- 虚线间隔宽度
</shape>
注意:
一、<corners>
1、android:radius,半径,会被下边的单个角度半径属性覆盖,默认为1dp,
2、在使用时,如果单独设置四个角度,又大小不一致时,eclipse的graphics preview会报错。但是直接真机运行即可。(比如实线上边直角,下边屈角的效果)
二、<gradient>中的角度 android:angle是从左开始,逆时针旋转。例如angle=“0”时,渐变色是从左至右;angle="90",渐变色是从下至上;angle=“180”,渐变色是从右至左;angle="270",渐变色是从上至下。
1.渐变色效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="180"
android:startColor="#f00"
android:centerColor="#000"
android:endColor="#ff0"
/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
2、白色边框、半透明效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="16dp" />
<!-- 这是半透明,还可以设置全透明,那就是白色边框的效果了 -->
<solid android:color="#80ff0000" />
<stroke
android:dashGap="0dp"
android:width="4dp"
android:color="@android:color/white" />
</shape>
3、分割线效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:width="4dp"
android:color="@android:color/black" />
</shape>
效果如下:
![](http://img.blog.csdn.net/20160530205220656?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
其实工作中很多设计效果都可以用shape来实现,既方便又快捷!
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] > --- 默认为rectangle
<corners -- shape=“rectangle”时使用,
android:radius="integer" -- 半径,会被下边的属性覆盖,默认为1dp,
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient -- 渐变
android:angle="integer"
android:centerX="integer"
android:centerY="integer"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size -- 指定大小,一般用在imageview配合scaleType属性使用。大小一般会适配滴
android:width="integer"
android:height="integer" />
<solid -- 填充颜色,可是是十六进制颜色。(比如想设置半透明效果,直接使用十六就只就OK)
android:color="color" />
<stroke -- 指定边框,border,dashWidth和dashGap有一个为0dp则为实线
android:width="integer"
android:color="color"
android:dashWidth="integer" -- 虚线宽度
android:dashGap="integer" /> -- 虚线间隔宽度
</shape>
注意:
一、<corners>
1、android:radius,半径,会被下边的单个角度半径属性覆盖,默认为1dp,
2、在使用时,如果单独设置四个角度,又大小不一致时,eclipse的graphics preview会报错。但是直接真机运行即可。(比如实线上边直角,下边屈角的效果)
二、<gradient>中的角度 android:angle是从左开始,逆时针旋转。例如angle=“0”时,渐变色是从左至右;angle="90",渐变色是从下至上;angle=“180”,渐变色是从右至左;angle="270",渐变色是从上至下。
1.渐变色效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="180"
android:startColor="#f00"
android:centerColor="#000"
android:endColor="#ff0"
/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="8dp" />
</shape>
2、白色边框、半透明效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="16dp" />
<!-- 这是半透明,还可以设置全透明,那就是白色边框的效果了 -->
<solid android:color="#80ff0000" />
<stroke
android:dashGap="0dp"
android:width="4dp"
android:color="@android:color/white" />
</shape>
3、分割线效果:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >
<stroke
android:width="4dp"
android:color="@android:color/black" />
</shape>
效果如下:
其实工作中很多设计效果都可以用shape来实现,既方便又快捷!
相关文章推荐
- OpenglES2.0 for Android:来画个三角形吧
- Android windowSoftInputMode属性解析
- android 6.0sd卡内部存储 & 外部存储
- Android基础知识整合篇——Activity知识点
- Android小白入坑之行
- Android 4000 学习记录(十九)-简单的动画清除
- Android简单自定义圆形和水平ProgressBar
- Android系统无法进入休眠
- 由于drawable目录下新建文件夹导致的AndroidStudio编译失败解决思路!
- android中selector在java代码中使用无效的解决方案
- Android:Intent Filter 和 action 简介AndroidMainfest.xm
- Android学习记录(十八)-url转义继续填坑之旅
- Android开发 EditTextPreference 和 ListPreference的summary值动态修改
- 从Android代码中来记忆23种设计模式
- service全面解析(一)
- Android 在内部存储读写文件
- 中兴部分手机BLE不能带参数扫描
- Android—基于GifView显示gif动态图片
- 论工程结构设计的重要性
- Android Espresso 测试框架探究