您的位置:首页 > 其它

自定义View如何定义和使用自己的属性

2012-10-04 21:04 337 查看
自定义View如何定义自己的属性和使用自己的属性
在Android自定义View实现很简单,继承View,重写构造函数、onDraw,(onMeasure)等函数。

如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性,该属性没有默认值,只是说明该View包含的一些自定义的属性。

设置的属性可以是android系统本身存在一些属性,这些属性可以不用设置属性的类型format。当时自己设置的属性,必须设置属性的format。因为涉及到Layout设置值的format和在自定义的View中获得不同的format使用不同的方法。

在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径".

在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。

主程序:

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class Test extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_test, menu);
return true;
}

}


自定义View程序:

package com.example.test;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

/**
* 这个是自定义的TextView.
* 至少需要重载构造方法和onDraw方法
* 对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了
* 如果含有自己独特的属性,需要设置属性的format 在Layout中并根据需要设定值
* 如果使用自定义属性,那么在应用xml文件中需要加上新的schemas,
* 比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
* 其中xmlns后的“my”是自定义的属性的前缀,res后的是我们自定义View所在的包
* @author Administrator
*
*/
public class MyText extends View {

private Paint mPaint;

public MyText(Context context) {
// TODO Auto-generated constructor stub
super(context);
}

public MyText(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
/*
* @param AttributeSet代表Layout布局中设置的控件的属性值
* @param int[] 表示要获取的属性值
* @return TypedArray 获取的属性集合		 */
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyText);
//textColor的值可以在Layout的XML中设置,但是在Layout的XML中的schemas必须是为
//xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径",这样就可以设置属性值。
//getColor的第一个参数为XML中的属性,第二个为默认值
int textColor = array.getColor(R.styleable.MyText_textColor, 0xff000000);
float textSize = array.getDimension(R.styleable.MyText_textSize, 30);
mPaint.setColor(textColor);
mPaint.setTextSize(textSize);
array.recycle();
}

public void draw(Canvas canvas){
super.draw(canvas);
mPaint.setStyle(Style.FILL);
canvas.drawRect(10, 10, 100, 100, mPaint);
mPaint.setColor(Color.BLUE);
canvas.drawText("画出来的文字", 10, 120, mPaint);

}

}


自定义View的属性列表attrs.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyText">
<attr name="textColor" format="color"/>
<attr name="textSize" format="dimension"/>
</declare-styleable>

</resources>


布局文件layout_test.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:my="http://schemas.android.com/apk/res/com.example.test"
>
<com.example.test.MyText
android:layout_width="match_parent"
android:layout_height="match_parent"
my:textSize="25dp"
my:textColor="#a0a0a0a0"
/>

</LinearLayout>

在Layout布局中设置的textColor和在View中设置的默认值不同,可以试验改变一下值。应该可以明白不少。

在通过xml文件构造view组件的时候,往往都要使用到AttributeSet和defStyle这个两个参数,例如Button组件的构造方法Button(Context ctx, AttributeSet attrs, int defStyle)中,ctx会调用obtainStyledAttributes( AttributeSet set, int[] attrs, int defStyleAttr,
int defStyleRes)方法获得一个TypedArray,然后根据这个TypeArray来设置组件的属性。obtainStyledAttributes这类方法有好几个,真正的实现是Resources.Theme类,分别是:

(1) obtainStyledAttributes( AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes) : TypedArray

(2) obtainStyledAttributes( int resid, int[] attrs) : TypeArray

(3) obtainStyledAttributes(int[] attrs) : TypeArray

在方法(1)里根据attrs确定要获取哪些属性,然后依次通过其余3个参数来取得相应的属性值,属性值获取的优先级从高到低依次是set, defStyleAttr, defStyleRes. defStyleAttr是一个reference, 它指向当前Theme中的一个style, style其实就是各种属性的集合,如果defStyleAttr为0或者在Theme中没有找到相应的style,
则 才会尝试从defStyleRes获取属性值,defStyleRes表示的是一个style的id, 当它为0时也无效。方法(2)和(3)分别表示从style或Theme里获取属性值。

attr是在/res/values/attrs.xml文件下定义的,除了系统组件本身的属性,我们也可以自定义属性,然后在layout布局中使用。attrs.xml里通常包括若干个attr集合,例如

<declare-styleable name="LabelView">

<attr name="text" format="string" />

<attr name="textColor" format="color" />

<attr name="textSize" format="dimension" />

</declare-styleable>

就表示一个attr集合,declare-styleable标签里的name值表示的就是上面方法里的attrs参数,android会自动在R文件中生成一个数组, 它可以使任意的不一定要是view组件名称。在集合里定义每个属性的名称和它的类型,据偶所见总共有reference, string, color, dimension, boolean等,如果允许多个类型可以用"|"来隔开,比如reference
| color, attr还可以这样定义

<attr name="layout_height" format="dimension">

<enum name="fill_parent" value="-1" />

<enum name="match_parent" value="-1" />

<enum name="wrap_content" value="-2" />

</attr>

当attr的定义没有指明format时,表示它已经在其他地方定义过了,所以你可以定义一个attr集合,里面的都是已经定义好的属性(例如系统组件的属性), 然后通过obtainStyledAttributes方法来获取这些属性值,例如

<declare-styleable name="Gallery1">

<attr name="android:galleryItemBackground" />

</declare-styleable>

在layout布局中使用自定义的属性,要指明包名称,需要先定义,例如xmlns:app="http://schemas.android.com/apk/res/your_package_name", 然后就可以这样app:text, app:textSize来设置属性了。

R文件中会有styleable和attr这两个类,当我们要使用哪个属性集合或哪个属性的时候用的是styleable, 而attr类定义的仅仅是attr这个属性在layout中的id. AttributeSet有两个方法分别是

int getAttributeNameResource(int index);

int getAttributeResourceValue(int index, int defaultValue);

前一个方法获取的就是attr属性名称的id,也也就是attr类定义的数值,后一个方法获取的才是attr属性值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐