Android自定义View之资源Attr
来源:互联网 发布:上海软件工作室 编辑:程序博客网 时间:2024/06/13 23:39
Android开发中自定义View时会经常使用到自定义属性,或者在常规开发中也会经常用到各种控件的属性进行样式及不同效果的开发,下面是定义一个属性时需要知道的东西.
1 定义:
1 文件定义,在res/values/目录下定义一个Android资源xml,一般命名为attrs.xml;
内容如下:
<?xml version="1.0" encoding="utf-8"?><resources></resources>
2 属性定义
通过标签定义一个属性,attr有两个属性,分别是:
name,用于申明该属性的名字
format,用于限制该属性可以被赋值的数据类型,可选类型为:
- reference:参考某一资源ID。
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "background" format = "reference" /> </declare-styleable>
(2)属性使用:
<ImageView Android:layout_width = "42dip" android:layout_height = "42dip" android:background = "@drawable/图片ID" />
- color:颜色值。
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "textColor" format = "color" /> </declare-styleable>
(2)属性使用:
<TextView android:layout_width = "42dip" android:layout_height = "42dip" android:textColor = "#00FF00" />
- boolean:布尔值。
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "focusable" format = "boolean" /> </declare-styleable>
(2)属性使用:
<Button android:layout_width = "42dip" android:layout_height = "42dip" android:focusable = "true" />
- dimension:尺寸值。
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "layout_width" format = "dimension" /> </declare-styleable>
(2)属性使用:
<Button android:layout_width = "42dip" android:layout_height = "42dip" />
- float:浮点值。
(1)属性定义:
<declare-styleable name = "AlphaAnimation"> <attr name = "fromAlpha" format = "float" /> <attr name = "toAlpha" format = "float" /> </declare-styleable>
(2)属性使用:
<alpha android:fromAlpha = "1.0" android:toAlpha = "0.7" />
- integer:整型值。
(1)属性定义:
<declare-styleable name = "AnimatedRotateDrawable"> <attr name = "visible" /> <attr name = "frameDuration" format="integer" /> <attr name = "framesCount" format="integer" /> <attr name = "pivotX" /> <attr name = "pivotY" /> <attr name = "drawable" /> </declare-styleable>
(2)属性使用:
<animated-rotate xmlns:android = "http://schemas.android.com/apk/res/android" android:drawable = "@drawable/图片ID" android:pivotX = "50%" android:pivotY = "50%" android:framesCount = "12" android:frameDuration = "100" />
- string:字符串。
(1)属性定义:
<declare-styleable name = "MapView"> <attr name = "apiKey" format = "string" /> </declare-styleable>
(2)属性使用:
<com.google.android.maps.MapView android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:apiKey = "0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />
- fraction:百分数。
(1)属性定义:
<declare-styleable name="RotateDrawable"> <attr name = "visible" /> <attr name = "fromDegrees" format = "float" /> <attr name = "toDegrees" format = "float" /> <attr name = "pivotX" format = "fraction" /> <attr name = "pivotY" format = "fraction" /> <attr name = "drawable" /> </declare-styleable>
(2)属性使用:
<rotate xmlns:android = "http://schemas.android.com/apk/res/android"
android:interpolator = “@anim/动画ID”
android:fromDegrees = "0"
android:toDegrees = “360”
android:pivotX = "200%" android:pivotY = "300%"
android:duration = “5000”
android:repeatMode = "restart" android:repeatCount = "infinite" />
- enum:枚举值。
(1)属性定义:
<declare-styleable name="名称"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable>
(2)属性使用:
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent" > </LinearLayout>
flag:位或运算。
(1)属性定义:
<declare-styleable name="名称"> <attr name="windowSoftInputMode"> <flag name = "stateUnspecified" value = "0" /> <flag name = "stateUnchanged" value = "1" /> <flag name = "stateHidden" value = "2" /> <flag name = "stateAlwaysHidden" value = "3" /> <flag name = "stateVisible" value = "4" /> <flag name = "stateAlwaysVisible" value = "5" /> <flag name = "adjustUnspecified" value = "0x00" /> <flag name = "adjustResize" value = "0x10" /> <flag name = "adjustPan" value = "0x20" /> <flag name = "adjustNothing" value = "0x30" /> </attr> </declare-styleable>
(2)属性使用:
<activity android:name = ".StyleAndThemeActivity" android:label = "@string/app_name" android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden"> <intent-filter> <action android:name = "android.intent.action.MAIN" /> <category android:name = "android.intent.category.LAUNCHER" /> </intent-filter> </activity>
注意:
属性定义时可以指定多种类型值。
(1)属性定义:
<declare-styleable name = "名称"> <attr name = "background" format = "reference|color" /> </declare-styleable>
(2)属性使用:
<ImageView android:layout_width = "42dip" android:layout_height = "42dip" android:background = "@drawable/图片ID|#00FF00" />
3 属性分类
属性分为全局属性和局部属性,类似Java类中的全局变量和局部变量,其中全局属性是每一个View在自定义属性的时候都可以使用的,局部属性是某个View自己定义来自己使用的.
- 全局属性,比如有两个View都需要一个叫做maxHeight的属性来控制其显示的最大高度,在定义的时候可以将这个属性定义为全局的.
<?xml version="1.0" encoding="utf-8"?><resources> <!-- 将该属性定义为全局的--> <attr name="maxHeight" format="dimension" /> <!-- View1 和View2对应的自定义属性中都引用了全局的maxHeight属性,不需要再重新指定format--> <declare-styleable name="View1"> <attr name="maxHeight" /> </declare-styleable> <declare-styleable name="View2"> <attr name="maxHeight" /> </declare-styleable></resources>
- 布局属性,类似Java中的局部变量,例如需要给一个View定义一个src属性用于指定显示的图片
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="View1"> <!-- 定义了一个src属性,该属性只能在View1中使用--> <attr name="src" format="reference|color" /> </declare-styleable></resources>
4 属性编译后的形式
Android项目的资源编译后会生成一个R.java文件,其中每一种资源类型会生成一个静态内部类,如下图所示;
其中属性被定义在内部类attr下:
全局属性,全局属性直接生成一个整型变量,直接给它赋一个唯一的资源ID值.
public static final int absListViewStyle=0x0101006a;
局部属性,局部属性和全局属性不同
public static final int[] ImageView = { 0x01010119, 0x0101011d, 0x0101011e, 0x0101011f, 0x01010120, 0x01010121, 0x01010122, 0x01010123, 0x0101031c, 0x010103fb, 0x01160051 }; public static final int ImageView_adjustViewBounds = 2; public static final int ImageView_baseline = 8; public static final int ImageView_baselineAlignBottom = 6; public static final int ImageView_cropToPadding = 7; public static final int ImageView_drawableAlpha = 10; public static final int ImageView_maxHeight = 4; public static final int ImageView_maxWidth = 3; public static final int ImageView_scaleType = 1; public static final int ImageView_src = 0; public static final int ImageView_tint = 5; public static final int ImageView_tintMode = 9;
局部对象首先会生成一个数组,该数组的值就是在declare-styleable标签中申明的属性的资源ID值.
然后根据declare-styleable的name属性和该declare-styleable下申明的属性名生成一个整型变量,该变量的值就是其在
申明时的顺序,从0开始.该变量的值和上面生成的数组的Index一一对应,就是上面生成数组的索引.
public static final int ImageView_tintMode = 9;
- Android自定义View之资源Attr
- Android自定义View 之attr
- Android自定义View之属性(Attr)解析
- android 自定义view attr
- android自定义控件之Attr
- Android 自定义view (一)——attr 理解
- Android 自定义view(二) —— attr 使用
- Android自定义View的属性,attr format取值类型
- Android 自定义View 中attr属性 深入解析
- Android之自定义View
- android 之自定义View
- Android之自定义View
- Android之自定义View
- Android之自定义View
- Android 之 自定义View
- Android 自定义 attr属性
- Android 自定义View 之 自定义View属性
- Android 自定义View的资源回收
- 【echarts】无数据时气泡的修改
- 20161219
- Android 反编译初探 应用是如何被注入广告的
- 基于webpack搭建前端工程解决方案探索
- 简单小编程
- Android自定义View之资源Attr
- ASP网站访问量计数器
- Redis与Memcached的区别
- 对mvc及三层架构的个人理解
- 编写一个程序,最多将10个donation值读入到一个double数组中。程序遇到非数字输入时将结束输入,并报告这些数字的平均值以及数组中有多少个数字大于平均值。
- 博客迁移
- css
- Linux开发调试经验整理(一)——Release版本死锁定位
- angular2.0如何定义属性指令