您的位置:首页 > 其它

自定义属性

2015-06-04 14:50 267 查看
自定义属性

自定义属性:一般自定义控件和视图时用到自定义的属性。我个人感觉自定义属性用在自定义的view上显得封装性很好,比如这个属性就是属于这个控件用的,显得很自然。就好比系统控件用的是系统定义的属性。这个自定义属性 我们可以仿照系统定义的属性来做,系统定义的放在frameworks/base/core/res/res/values/attrs.xml文件中。具体步骤大致可以分为:

1:声明属性

2:新建自定义view获取声明的属性

3:在xml布局文件中结合自定义的view来使用自定义属性

4: 在新建的view类中获取自定义属性


<strong><span style="font-size:24px;">1:声明属性:</span></strong>
在values目录下新建attrs.xml文件。在文件中编写声明属性。

<?xml version="1.0" encoding="utf-8"?>
<resource>
//用declare-styleable标签来声明属性集即可,name是名称,可以自定义。这个名称就是我们引用自定义属性时的入口。
//下面的attr来声明具体的属性。同样那么是属性名称,而format是这个属性是声明类型,这个类型就是我们用时的数据类型。
比如string,int,等等,还可以是引用类型reference,比如Drawable等

<declare-styleable name="属性集名称">
<attr name="属性名" format="属性定义的类型"/>
</declare-styleable>

<!-- 声名属性集的名称 -->
<declare-styleable name="MyView">

<!-- 声名一个属性  name是test_id   类型为 整数类型 -->
<attr name="test_id" format="integer" />

<!-- 声名一个属性  name是test_msg   类型为 字符串类型 -->
<attr name="test_msg" format="string" />

<!-- 声名一个属性  name是test_bitmap   类型为 引用类型      引用资源ID -->
<attr name="test_bitmap" format="reference" />

</declare-styleable>

<!--例如:-->
<declare-styleable name="MyView">
<attr name="width" format="demensions"/>
<attr name="text" format="string"/>
</declare-styleable>

</resources>


<span style="font-size:24px;">2:新建自定义view类。</span>

public MyView extends View{

//必须复写带两个参数的构造方法
public MyView(Context context,AttributeSet attrs){
System.out.println("调用构造test");
}
}


3:在xml布局文件中使用自定义属性。
在使用之前首先要 引入命名空间,例如:

xmlns:testnamespace="http://schemas.android.com/apk/res/com.example.hello"

这一段是固定的,前面可以自定义空间名。这里我定义为testnamespace
xmlns:testnamespace="http://schemas.android.com/apk/res/
这个是当前应用的包名
com.example.hello
引入命名空间之后就可以使用自定义属性了

<com.XXX.ui.test.MyView

android:id="@+id/testview"
android:layout_width="20dp"
android:layout_height="30dp"
testnamespace:text="niuzhihua"
testnamespace:age="20.5"
testnamespace:bg="@drawable/ic_launcher"
/>


至此为止,当我们在布局文件中用自定义控件时候,activity会加载此布局文件,初始化控件,这时就会调用public MyView(Context context,AttributeSet attrs)构造我们运行应用,便可以看到在Myview类中构造执行了。打印出字符串。

4:获取自定义的属性 并在自定义控件中使用。

上面我们看到有public MyView(Context context,AttributeSet attrs)构造。发现有个AttributeSet类,根据名字可以了解到是“属性集合”,这个类就封装了我们的自定义属性。(将attr.xml解析后的 结果封装)

int count = attrs.getAttributeCount();
System.out.println(count);
for(int i=0;i<count;i++){

String attrName = attrs.getAttributeName(i);
String attrValue = attrs.getAttributeValue(i);
System.out.println(attrName+"-"+attrValue);
}


我们可以根据这个AttributeSet来获取自定义属性并使用,例如我们拿到testnamespace:text 属性并可以得到值(通过反射等等),但是用AttributeSet有点儿麻烦。android系统里面给我们提供了更好的方式:

TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MyView);

通过这个TypedArray对象就可以获取

public MyView(Context context,AttributeSet attrs) :

System.out.println("-------------------------");
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.MyView);
int indexcount = ta.getIndexCount();
for(int i=0;i<indexcount;i++){
//这个就是自定义属性名  的id了
int idName = ta.getIndex(i);
//这个 是属性值的 id
int idValue = ta.getResourceId(idName, -8);//-8表示获取不到资源id

System.out.println("属性名id:"+idName+"属性值id:"+idValue);
//那么我们实际上需要的是属性值,这里通过属性名来得到属性值。
switch (idName) {
case R.styleable.MyView_text:
break;
case R.styleable.MyView_bg:
Drawable d = ta.getDrawable(idName);
//经过测试 基本数据类型是没有属性值id的 。
System.out.println("属性名:MyView_bg--属性名id:"+"属性值--"+d+idName+"--属性值id:"+idValue);
System.out.println(d);
break;
case R.styleable.MyView_age:
break;
}
}


最后注:自定义属性还有一种不正规的用法。那就是直接在布局文件中使用,不用引入命名空间。例如:

<com.xxx.ui.test.MyView

android:id="@+id/testview"
android:layout_width="20dp"
android:layout_height="30dp"
testnamespace:text="niuzhihua"
testnamespace:age="20.5"
testnamespace:bg="@drawable/ic_launcher"
abc="不正规的用法"
abb="@drawable/ic_launcher"
/>


解析属性时用:

public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
System.out.println("不正规用法--------------------------");
String abcValue = attrs.getAttributeValue(null, "abc");
String abbValue = attrs.getAttributeValue(null, "abb");
//运行结果abc:不正规的用法 abb:@drawable/ic_launcher
System.out.println("abc:"+abcValue+" abb:"+abbValue);
//那么我们可以通过截取得到abb的名称ic_launcher
int id = context.getResources().getIdentifier("ic_launcher","drawable" , "com.example.hello");
Drawable d2 = context.getResources().getDrawable(id);
System.out.println(d2);
//d2也是有值的,结果为: android.graphics.drawable.BitmapDrawable@41a6b1b8

}


这样写也可以。

运行打印结果: abc:不正规的用法 abb:@drawable/ic_launcher
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: