Android 自定义View并添加属性介绍

[摘要]本文主要介绍如何为自定义的View添加属性以及属性的类型,并提供简单的示例代码供参考。

本文主要介绍如何为自定义的View添加属性以及属性的类型。代码示例定义见DropDownToRefreshListView,调用见DropDownToRefreshListViewDemo.

1、添加自定义View的属性文件

在res/values中新建attrs.xml文件(文件名可另取,不过推荐用attrs.xml,可以将自定义属性都放入其中),内容为:

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 <declare-styleable name="myViewDefinedAttr">
4 <attr name="attr1" format="boolean"/>
5 <attr name="attr2" format="integer"/>
6 </declare-styleable>
7 </resources>

定义名为myViewDefinedAttr的属性列表,这个name命名也可以用下划线形式。name会在下面第二步中使用。

<attr name="attr1" format="boolean"/> 其中name为属性名,format为属性类型,可以为boolean, string, integer, dimension, float, reference, color, fraction, enum, flag及其混合。具体定义及调用方式见本文最后关于自定义属性的介绍。

2、自定义View中获取属性值

1 public class MyView extends View {
2
3 private boolean attr1;
4 private int attr2;
5
6 public MyView(Context context){
7 super(context);
8 }
9
10 public MyView(Context context, AttributeSet attrs){
11 super(context, attrs);
12 getAttrs(context, attrs);
13 }
14
15 public MyView(Context context, AttributeSet attrs, int defStyle){
16 super(context, attrs, defStyle);
17 getAttrs(context, attrs);
18 }
19
20 /**
21 * 得到属性值
22 *
23 * @param context
24 * @param attrs
25 */
26 private void getAttrs(Context context, AttributeSet attrs) {
27 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myViewDefinedAttr);
28 attr1 = ta.getBoolean(R.styleable.myViewDefinedAttr_attr1, true);
29 attr2 = ta.getInt(R.styleable.myViewDefinedAttr_attr2, 0);
30 ta.recycle();
31 }
32 }

从上面可以看出,主要是通过context.obtainStyledAttributes得到属性及其值的对应列表。

ta.getBoolean(R.styleable.myViewDefinedAttr_attr1, true);表示得到属性名为attr1的boolean值,若不存在该属性,则默认为true。这里的R.styleable.myViewDefinedAttr_attr1为第一步中的属性列表名_属性名。
ta.recycle();为回收系统资源。

3、调用自定义View

1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 xmlns:myViewXmlns="http://schemas.android.com/apk/res/com.trinea.mypackage"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent" >
6
7 <com.trinea.android.common.view.MyView
8 android:id="@+id/app_list"
9 android:layout_width="match_parent"
10 android:layout_height="match_parent"
11 android:fastScrollEnabled="true"
12 myViewXmlns:attr1="false"
13 myViewXmlns:attr2="1"
14 android:focusable="true" />
15
16 ……
17 </RelativeLayout>

调用的主体为<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表示枚举值,定义为:

1 <attr name="enum_attr">
2 <enum name="horizontal" value="0" />
3 <enum name="vertical" value="1" />
4 </attr>

调用如 xx:attr1="horizontal"。

(10) flag表示位或运算,定义为:

1 <attr name="windowSoftInputMode">
2 <flag name = "stateUnspecified" value = "0" />
3 <flag name = "stateUnchanged" value = "1" />
4 <flag name = "stateHidden" value = "2" />
5 <flag name = "stateAlwaysHidden" value = "3" />
6 <flag name = "stateVisible" value = "4" />
7 <flag name = "stateAlwaysVisible" value = "5" />
8 <flag name = "adjustUnspecified" value = "0x00" />
9 <flag name = "adjustResize" value = "0x10" />
10 <flag name = "adjustPan" value = "0x20" />
11 <flag name = "adjustNothing" value = "0x30" />
12 </attr>

调用如:xx:attr1="stateUnspecified | stateUnchanged | stateHidden"。

(11) 混合类型,定义为:

1 <declare-styleable name = "combine_type">
2 <attr name = "background" format = "reference|color" />
3 </declare-styleable>

调用如 xx:attr1 = "@drawable/图片ID|#DDFF00"。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值