Android自定义View在xml中定义样式可以定义的类型
2015-08-05 10:18
399 查看
在编写Android的自定义View时,需要在布局中使用定义的样式属性来动态控制显示效果,那么定义样式属性的值有有哪些呢,下面是我总结的一些类型:
attr可以定义和引用,定义的时候有名称和类型,引用的时候可以直接使用<attr name="attr名称" />
在定义样式属性的时候,会用到fromat来定义属性类型,下面来介绍各种类型的值:
color:颜色值类型,值为#ff000000形式
<attr name="myColor" format="color" />
integer:整型
<attr name="myInteger" format="integer" />
float:浮点型
<attr name="myAlpha" format="float" />
boolean:布尔值
<attr name="hasColor" format="boolean" />
dimension:尺寸,取值可以是dp,sp,px
<attr name="myDim" format="dimension" />
string: 字符串
<attr name="myString" format="string" />
reference : 引用类型,包含res中可以引用的资源
<attr name="myRef" format="reference" />
flag:标识,赋值的时候只能是flag的name,但是当添加format的时候可以接受format里的值,编译可以通过
<attr name="myTag" >
<flag name="one" value="1"/>
<flag name="tow" value="2"/>
</attr>
enum:枚举,赋值的时候只能是enmu的name
<attr name="typeface" format="enum ">
<enum name="normal" value="0" />
<enum name="sans" value="1" />
<enum name="serif" value="2" />
<enum name="monospace" value="3" />
</attr>
但是当添加format的时候可以接受format里的值[b][b],编译可以通过,如设置宽高的时候可以设置尺寸,也可以设置枚举类型的wrap_content和match_parent等,但是这些枚举在宽高里面都有定义[/b][/b]
<attr name="layout_width" format="dimension">
<!-- The view should be as big as its parent (minus padding).
This constant is deprecated starting from API Level 8 and
is replaced by {@code match_parent}. -->
<enum name="fill_parent" value="-1" />
<!-- The view should be as big as its parent (minus padding).
Introduced in API Level 8. -->
<enum name="match_parent" value="-1" />
<!-- The view should be only big enough to enclose its content (plus padding). -->
<enum name="wrap_content" value="-2" />
</attr>
fraction:百分比,如50%
<attr name="myFraction" value="fraction"/>
attr可以定义和引用,定义的时候有名称和类型,引用的时候可以直接使用<attr name="attr名称" />
在定义样式属性的时候,会用到fromat来定义属性类型,下面来介绍各种类型的值:
color:颜色值类型,值为#ff000000形式
<attr name="myColor" format="color" />
integer:整型
<attr name="myInteger" format="integer" />
float:浮点型
<attr name="myAlpha" format="float" />
boolean:布尔值
<attr name="hasColor" format="boolean" />
dimension:尺寸,取值可以是dp,sp,px
<attr name="myDim" format="dimension" />
string: 字符串
<attr name="myString" format="string" />
reference : 引用类型,包含res中可以引用的资源
<attr name="myRef" format="reference" />
flag:标识,赋值的时候只能是flag的name,但是当添加format的时候可以接受format里的值,编译可以通过
<attr name="myTag" >
<flag name="one" value="1"/>
<flag name="tow" value="2"/>
</attr>
enum:枚举,赋值的时候只能是enmu的name
<attr name="typeface" format="enum ">
<enum name="normal" value="0" />
<enum name="sans" value="1" />
<enum name="serif" value="2" />
<enum name="monospace" value="3" />
</attr>
但是当添加format的时候可以接受format里的值[b][b],编译可以通过,如设置宽高的时候可以设置尺寸,也可以设置枚举类型的wrap_content和match_parent等,但是这些枚举在宽高里面都有定义[/b][/b]
<attr name="layout_width" format="dimension">
<!-- The view should be as big as its parent (minus padding).
This constant is deprecated starting from API Level 8 and
is replaced by {@code match_parent}. -->
<enum name="fill_parent" value="-1" />
<!-- The view should be as big as its parent (minus padding).
Introduced in API Level 8. -->
<enum name="match_parent" value="-1" />
<!-- The view should be only big enough to enclose its content (plus padding). -->
<enum name="wrap_content" value="-2" />
</attr>
fraction:百分比,如50%
<attr name="myFraction" value="fraction"/>
相关文章推荐
- 六款值得推荐的android(安卓)开源框架简介
- 配置android source 在ubuntu中编译环境
- android的编译和运行过程深入分析
- android建立文件分享
- Android的ProgressBar
- Android---62---DrawerLayout实现抽屉效果
- Android API 中文 (15) —— GridView属性
- Android SearchView 修改hint颜色和搜索字的颜色
- android 在一个activity中杀死另外一个Activity
- android:descendantFocusability用法简析
- Android之Notificatoin使用
- android应用内切换语言(无需退出应用)
- 别踩白块儿游戏源码Android版
- android继续探索Fresco
- Android PorterDuff.Mode与Canvas实际使用
- android JNI C代码对sdcard中文件的操作
- 智力火柴游戏Android源码项目
- 智力火柴游戏Android源码项目
- Android根据xml配置文件动态修改九宫格功能配置
- Android调试工具_ Stetho