Android 中自定义属性主要有,明确需要一些什么样的属性,在values文件夹中创建attrs.xml文件,如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="myView">
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
创建好以上文件,所定义好的属性会在R.java中出现(有时候会出现Bug,真心无解!)。
由于出现自定义属性,一般情况下,我们会自定义一个View的,如下
package com.example.myview.ui;
import com.example.myview.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
private Paint myPaint;
private static final String myString = "Welcome to our Zoon!";
public MyView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyView(Context context, AttributeSet attr) {
super(context, attr);
myPaint = new Paint();
TypedArray a = context.obtainStyledAttributes(attr, R.styleable.myView);// TypedArray是一个数组容器
float textSize = a.getDimension(R.styleable.myView_textSize, 30);// 防止在XML文件里没有定义,就加上了默认值30
int textColor = a.getColor(R.styleable.myView_textColor, 0xFFFFFFFF);// 同上,这里的属性是:名字_属性名
myPaint.setTextSize(textSize);
myPaint.setColor(textColor);
a.recycle();// 我的理解是:返回以前取回的属性,供以后使用。以前取回的可能就是textSize和textColor初始化的那段
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// myPaint = new Paint();
myPaint.setColor(Color.RED);
myPaint.setStyle(Style.FILL);
canvas.drawRect(new Rect(10, 10, 100, 100), myPaint);
myPaint.setColor(Color.WHITE);
canvas.drawText(myString, 10, 100, myPaint);
}
}
在自定义的View中构造方法是必须要有的,在其中的构造方法中,如果需要用到对属性进行操作的话可以获取属性,有时候也不必在此构造方法中获取用到,自定义的属性可以在布局文件中用到,如果在java代码中不对属性操作的话,没必要获取属性,但是,往往是需要进行处理的。
再看看布局文件activity_main.xml,如下,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.example.myview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<com.example.myview.ui.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
test:textColor="#fff"
test:textSize="10px" />
</LinearLayout>
对于xmlns:android="http://schemas.android.com/apk/res/android"我们都比较熟悉,但是不常看到而已,只是自动生成的。xmlns:android声明命名空间是android。何为命名空间?里面定义了各个类所用的属性的定义。上面有两个命名空间,分别为android和test,android这个命名空间就对应了/frameworks/base/core/res/res/values/attrs.xml文件中定义的属性值;至于test命名空间,在工程目录下的/res/values/attrs.xml,attrs.xml是我们创建的。对于自定义属性给出的命名空间后面的值,即为url,是有规则的,先看这一条官方的xmlns:android="http://schemas.android.com/apk/res/android",我们只需将这一条复制在其下面,将xmlns:android中的android改为自己想要的一个命名空间(随便取),但是对于后面的值"http://schemas.android.com/apk/res/android"需要慎重点,否则会报错,说什么自定义的属性名找不到,因此需要将"http://schemas.android.com/apk/res/android"中的最后一个android替换为自己工程中R.java所在包的包名,比如:xmlns:test="http://schemas.android.com/apk/res/com.example.myview"。在布局中使用自定义的View,应该使用该View的包名(这个大家应该都知道),如:
<com.example.myview.ui.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
test:textColor="#fff"
test:textSize="10px" />
定义了命名空间之后,在布局中就使用他,test:textColor="#fff",test:textSize="10px"。