android自定义属性
来源:互联网 发布:js设置cookie作用域 编辑:程序博客网 时间:2024/05/28 04:54
本文参考:http://blog.csdn.net/u013205623/article/details/53580426
一、自定义控件属性
自定义控件属性使用:
1,在values目录下新建atttrs.xml文件;【文件较多时,可实现分包新建 attrs_register.xml】
2,声明属性详情;
3,自定义控件构造方法中获取属性,并在实际构建View时使用。
示例:
新建attrs.xml文件,声明属性详情:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="SelfDefineTextView"> //属于指定控件名的属性 <attr name="name" format="string" /> <attr name="attr" format="enum"> <enum name="desc" value="0" /> <enum name="detail" value="1" /> </attr> <attr name="work" format="boolean" /> <attr name="color1" format="reference|color" /> <attr name="state"> <flag name="young" value="18" /> <flag name="adult" value="28" /> <flag name="old" value="60" /> </attr> <attr name="size" format="dimension" /> <!--分数--> <attr name="fraction" format="fraction" /> <attr name="other" format="float" /> </declare-styleable> </resources>
获取属性并使用:
public SelfDefineTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SelfDefineTextView); String attr = typedArray.getString(R.styleable.SelfDefineTextView_attr); String name = typedArray.getString(R.styleable.SelfDefineTextView_name); int color = typedArray.getColor(R.styleable.SelfDefineTextView_color1, 0xFF45C01A); float fraction = typedArray.getFraction(R.styleable.SelfDefineTextView_fraction, 1, 1, 0.5f); float other = typedArray.getFloat(R.styleable.SelfDefineTextView_other, 0f); float size = typedArray.getDimension(R.styleable.SelfDefineTextView_size, 20f); String state = typedArray.getString(R.styleable.SelfDefineTextView_state); int state = typedArray.getInt(R.styleable.SelfDefineTextView_state, -1); boolean work = typedArray.getBoolean(R.styleable.SelfDefineTextView_work, false); typedArray.recycle(); this.setTextSize(size); this.setTextColor(color); this.setText( "attr:" + attr + "\n" + "name:" + name + "\n" + "fraction:" + fraction + "\n" + "other:" + other + "\n" + "state:" + state + "\n" + "work:" + work ); }
属性获取第二种方式:
/*第二种方式:*/ TypedArray typedArray1 = context.obtainStyledAttributes(attrs, R.styleable.SelfDefineTextView); int n = typedArray1.getIndexCount(); for (int i = 0; i < n; i++) { int attrStr = typedArray1.getIndex(i); switch (attrStr) { case R.styleable.SelfDefineTextView_name: String nameStr = typedArray1.getString(attrStr); break; } }
使用时:
<com.future.selfdefineview.view.SelfDefineTextView android:id="@+id/self_define_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" attrtest:attr="desc" attrtest:color1="#f91245ff" attrtest:fraction="10%" attrtest:name="自定义TextView" attrtest:other="96.88" attrtest:size="@dimen/text_size" attrtest:state="adult" attrtest:work="true">
细节注意点:
1,属性声明文件中该自定义控件的名字与自定义控件名相同;【可不一样,不建议】
最重要的一点是declare-styleable旁边有一个name属性,这个name的取值就是对应所定义的类名。即要为哪个类添加自定义的属性,那这个name属性的值就是哪个
————————–SelfDefineTextView【自定义控件名/属性命名】
2,属性获取时,参数变量是属性命名_详细属性 得方式;
————————–String attr = typedArray.getString(R.styleable.SelfDefineTextView_attr);
3,TypeArray对象使用完时,一定释放:
————————–typedArray.recycle();
4,属性集导入方式:
xmlns:attrstest=”http://schemas.android.com/apk/res-auto”
二、属性详解
属性取值类型有:
● reference:引用资源 ● string:字符串 ● Color:颜色 ● boolean:布尔值 ● dimension:尺寸值 ● float:浮点型 ● integer:整型 ● fraction:百分数 ● enum:枚举类型 ● flag:位或运算
- reference指的是从string.xml、drawable.xml、color.xml等引用过来的值
- flag是自己定义的,类似于 android:gravity=”top”
- dimension 指的是是从dimension.xml里引用过来的内容.注意,这里如果是dp那就会做像素转换
1,TypeArray获取属性值:
typedArray.getInt(int index, float defValue); typedArray.getDimension(int index, float defValue); typedArray.getBoolean(int index, float defValue); typedArray.getColor(int index, float defValue); typedArray.getString(int index) typedArray.getDrawable(int index); typedArray.getResources();
2,具体详情
(1)refrerence:参考某一资源ID【Drawable】
<declare-styleable name = "名称"> <attr name = "background" format = "reference" /> </declare-styleable>
<ImageView android:layout_width = "86dip" android:layout_height = "86dip" android:background = "@drawable/图片ID"/>
(2)color:颜色值
<declare-styleable name = "名称"> <attr name = "textColor" format = "color" /> </declare-styleable>
<TextView android:layout_width = "32dip" android:layout_height = "32dip" android:textColor = "#FF0000"/>
(3)boolean:布尔值,正确或者错误
<declare-styleable name = "名称"> <attr name = "focusable" format = "boolean" /> </declare-styleable>
<Button android:layout_width = "40dip" android:layout_height = "40dip" android:focusable = "true"/>
(4)dimension:尺寸值【43dp/sp/px】
<declare-styleable name = "名称"> <attr name = "layout_width" format = "dimension" /> </declare-styleable>
<Button android:layout_width = "66dip" android:layout_height = "88dip"/>
(5)float:小数值
<declare-styleable name = "AlphaAnimation"> <attr name = "fromAlpha" format = "float" /> <attr name = "toAlpha" format = "float" /> </declare-styleable>
<alpha android:fromAlpha = "1.0" android:toAlpha = "0.7"/>
(6)Integer:整数值
<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>
<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"/>
(7)string:字符串
<declare-styleable name = "SelfDefineTextView"> <attr name = "apiKey" format = "string" /> </declare-styleable>
<com.future.view.SelfDefineTextView android:layout_width = "fill_parent" android:layout_height = "fill_parent" android:apiKey = "jahkjgkanbvkjKNvlNLBnLnl" />
(8)fraction:百分数
<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>
<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"/>
(9)menu:枚举
<declare-styleable name="名称"> <attr name="orientation"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr> </declare-styleable>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" android:orientation = "vertical" android:layout_width = "fill_parent" android:layout_height = "fill_parent"> </LinearLayout>
(10)flag:位或运算
【类似menu】
<attr name="state"> <flag name="young" value="18" /> <flag name="adult" value="28" /> <flag name="old" value="60" /> </attr>
attrtest:state="adult"
【类似或运算:表示或者或者】
<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>
<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>
- Android自定义控件 自定义属性
- Android自定义view自定义属性
- Android 自定义控件 自定义属性
- Android自定义View-自定义属性
- android 自定义控件自定义属性
- Android自定义View-自定义属性
- android 自定义控件属性
- android自定义属性
- Android自定义属性
- android 里自定义属性
- android自定义属性
- Android自定义控件属性
- Android 中自定义属性
- Android自定义属性
- android 自定义控件属性
- android自定义属性
- android 自定义xml属性
- android使用自定义属性
- const修饰符
- 华为 提取不重复的整数
- 动态规划解决找零钱问题
- XML和JSON的区别
- mybatis map方式inset返回主键
- android自定义属性
- stm32 iap 跳到app后不进中断问题
- POJ 1236 Network of Schools(强联通缩点)
- jquery 循环获得数字并用逗号进行拼接,同事后台进行循环取出
- 如何正确使用光纤跳线?
- Android animation帧动画
- SFP光模块
- Servlet文件上传
- 4ipnet——完善的网络解决方案