Android 自定义View并添加属性
2014-08-05 21:38
417 查看
本文主要介绍如何为自定义的View添加属性以及属性的类型。代码示例定义见DropDownToRefreshListView,调用见DropDownToRefreshListViewDemo
转自(http://www.cnblogs.com/trinea/archive/2012/11/14/2768271.html)
1、添加自定义View的属性文件
在res/values中新建attrs.xml文件(文件名可另取,不过推荐用attrs.xml,可以将自定义属性都放入其中),内容为
按 Ctrl+C 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="myViewDefinedAttr">
<attr name="attr1" format="boolean"/>
<attr name="attr2" format="integer"/>
</declare-styleable>
</resources>
按 Ctrl+C 复制代码
定义名为myViewDefinedAttr的属性列表,这个name命名也可以用下划线形式。name会在下面第二步中使用。
<attr name="attr1" format="boolean"/> 其中name为属性名,format为属性类型,可以为boolean, string, integer, dimension, float, reference, color, fraction, enum, flag及其混合。具体定义及调用方式见本文最后关于自定义属性的介绍。
2、自定义View中获取属性值
按 Ctrl+C 复制代码
public class MyView extends View {
private boolean attr1;
private int attr2;
public MyView(Context context){
super(context);
}
public MyView(Context context, AttributeSet attrs){
super(context, attrs);
getAttrs(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
getAttrs(context, attrs);
}
/**
* 得到属性值
*
* @param context
* @param attrs
*/
private void getAttrs(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myViewDefinedAttr);
attr1 = ta.getBoolean(R.styleable.myViewDefinedAttr_attr1, true);
attr2 = ta.getInt(R.styleable.myViewDefinedAttr_attr2, 0);
ta.recycle();
}
}
按 Ctrl+C 复制代码
从上面可以看出,主要是通过context.obtainStyledAttributes得到属性及其值的对应列表。
ta.getBoolean(R.styleable.myViewDefinedAttr_attr1, true);表示得到属性名为attr1的boolean值,若不存在该属性,则默认为true。这里的R.styleable.myViewDefinedAttr_attr1为第一步中的属性列表名_属性名。
ta.recycle();为回收系统资源。
3、调用自定义View
按 Ctrl+C 复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:myViewXmlns="http://schemas.android.com/apk/res/com.trinea.mypackage"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.trinea.android.common.view.MyView
android:id="@+id/app_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fastScrollEnabled="true"
myViewXmlns:attr1="false"
myViewXmlns:attr2="1"
android:focusable="true" />
……
</RelativeLayout>
按 Ctrl+C 复制代码
调用的主体为<com.trinea.android.common.view.MyView ……/>中内容,需要注意的是跟上面代码类似在外面的布局文件中加入自己的命名空间再通过命名空间调用属性,即
xmlns:myViewXmlns="http://schemas.android.com/apk/res/com.trinea.mypackage",其中myViewXmlns为空间名可自取,值为http://schemas.android.com/apk/res/加当前应用的包名,即AndroidManifest.xml中manifest节点的package值,如下
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trinea.mypackage"
4、自定义属性的类型
format表示的属性类型可以为boolean, string, integer, dimension, float, reference, color, fraction, enum, flag及其混合。
(1) boolean表示布尔值,调用如 xx:attr1="false"
(2) integer表示整型,调用如 xx:attr1="1"
(3) dimension表示尺寸值,调用如 xx:attr1="42dp"
(4) float表示浮点型,调用如 xx:attr1="0.7"
(5) color表示颜色值,调用如 xx:attr1="#00FF00"
(6) string表示字符串,调用如 xx:attr1="#adbddd"
(7) reference表示参考某一资源id,调用如 xx:attr1 = "@drawable/图片ID"
(8) fraction表示百分数,调用如 xx:attr1="30%"
以上类型定义都为<attr name="attr1" format="xxxtype"/>
(9) enum表示枚举值,定义为
调用如 xx:attr1="horizontal"
(10) flag表示位或运算,定义为
按 Ctrl+C 复制代码
<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>
按 Ctrl+C 复制代码
调用如:xx:attr1="stateUnspecified | stateUnchanged | stateHidden"
(11) 混合类型,定义为
调用如 xx:attr1 = "@drawable/图片ID|#DDFF00"
转自(http://www.cnblogs.com/trinea/archive/2012/11/14/2768271.html)
1、添加自定义View的属性文件
在res/values中新建attrs.xml文件(文件名可另取,不过推荐用attrs.xml,可以将自定义属性都放入其中),内容为
按 Ctrl+C 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="myViewDefinedAttr">
<attr name="attr1" format="boolean"/>
<attr name="attr2" format="integer"/>
</declare-styleable>
</resources>
按 Ctrl+C 复制代码
定义名为myViewDefinedAttr的属性列表,这个name命名也可以用下划线形式。name会在下面第二步中使用。
<attr name="attr1" format="boolean"/> 其中name为属性名,format为属性类型,可以为boolean, string, integer, dimension, float, reference, color, fraction, enum, flag及其混合。具体定义及调用方式见本文最后关于自定义属性的介绍。
2、自定义View中获取属性值
按 Ctrl+C 复制代码
public class MyView extends View {
private boolean attr1;
private int attr2;
public MyView(Context context){
super(context);
}
public MyView(Context context, AttributeSet attrs){
super(context, attrs);
getAttrs(context, attrs);
}
public MyView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
getAttrs(context, attrs);
}
/**
* 得到属性值
*
* @param context
* @param attrs
*/
private void getAttrs(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myViewDefinedAttr);
attr1 = ta.getBoolean(R.styleable.myViewDefinedAttr_attr1, true);
attr2 = ta.getInt(R.styleable.myViewDefinedAttr_attr2, 0);
ta.recycle();
}
}
按 Ctrl+C 复制代码
从上面可以看出,主要是通过context.obtainStyledAttributes得到属性及其值的对应列表。
ta.getBoolean(R.styleable.myViewDefinedAttr_attr1, true);表示得到属性名为attr1的boolean值,若不存在该属性,则默认为true。这里的R.styleable.myViewDefinedAttr_attr1为第一步中的属性列表名_属性名。
ta.recycle();为回收系统资源。
3、调用自定义View
按 Ctrl+C 复制代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:myViewXmlns="http://schemas.android.com/apk/res/com.trinea.mypackage"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.trinea.android.common.view.MyView
android:id="@+id/app_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fastScrollEnabled="true"
myViewXmlns:attr1="false"
myViewXmlns:attr2="1"
android:focusable="true" />
……
</RelativeLayout>
按 Ctrl+C 复制代码
调用的主体为<com.trinea.android.common.view.MyView ……/>中内容,需要注意的是跟上面代码类似在外面的布局文件中加入自己的命名空间再通过命名空间调用属性,即
xmlns:myViewXmlns="http://schemas.android.com/apk/res/com.trinea.mypackage",其中myViewXmlns为空间名可自取,值为http://schemas.android.com/apk/res/加当前应用的包名,即AndroidManifest.xml中manifest节点的package值,如下
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trinea.mypackage"
4、自定义属性的类型
format表示的属性类型可以为boolean, string, integer, dimension, float, reference, color, fraction, enum, flag及其混合。
(1) boolean表示布尔值,调用如 xx:attr1="false"
(2) integer表示整型,调用如 xx:attr1="1"
(3) dimension表示尺寸值,调用如 xx:attr1="42dp"
(4) float表示浮点型,调用如 xx:attr1="0.7"
(5) color表示颜色值,调用如 xx:attr1="#00FF00"
(6) string表示字符串,调用如 xx:attr1="#adbddd"
(7) reference表示参考某一资源id,调用如 xx:attr1 = "@drawable/图片ID"
(8) fraction表示百分数,调用如 xx:attr1="30%"
以上类型定义都为<attr name="attr1" format="xxxtype"/>
(9) enum表示枚举值,定义为
<attr name="enum_attr"> <enum name="horizontal" value="0" /> <enum name="vertical" value="1" /> </attr>
调用如 xx:attr1="horizontal"
(10) flag表示位或运算,定义为
按 Ctrl+C 复制代码
<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>
按 Ctrl+C 复制代码
调用如:xx:attr1="stateUnspecified | stateUnchanged | stateHidden"
(11) 混合类型,定义为
<declare-styleable name = "combine_type"> <attr name = "background" format = "reference|color" /> </declare-styleable>
调用如 xx:attr1 = "@drawable/图片ID|#DDFF00"
相关文章推荐
- Android 自定义View并添加属性
- Android 自定义View并添加属性介绍
- Android 自定义View并添加属性
- 为android自定义View控件添加自定义的属性
- Android 自定义View并添加属性(转)
- Android自定义View 属性添加
- Android 自定义View并添加属性
- android 自定义view添加自定义xml属性
- Android 自定义View并添加属性
- [Android_Develop]自定义View添加XML属性
- android 自定义View研究(二) — 自定义控件添加属性
- Android 两种为自定义组件添加属性的使用方法和区别
- Android自定义View以及layout属性全攻略
- Android 自定义View消除锯齿实现图片旋转,添加边框及文字说明
- android 动态添加自定义TextView
- Android自定义View及自定义控件属性declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用
- Android应用程序添加自定义的property属性
- android自定义view属性
- android自定义View之自定义EditText(添加删除功能)
- Android基础教程——在TextView中显示Html 自定义标签,获取标签属性