您的位置:首页 > 移动开发 > Android开发

Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用

2016-03-08 00:09 531 查看
/article/5088884.html

Android 中自定义控件和属性(attr.xml,declare-styleable,TypedArray)的方法和使用

一、res/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>

二、 我们在MyView.java 代码编写如下,其中下面的构造方法是重点,我们获取定义的属性R.sytleable.MyView_textColor, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ) 防止我们在xml 文件中没有定义.从而使用默认值!
MyView 就是定义在<declare-styleable name="MyView "></declare-styleable> 里的 名字,获取里面属性用 名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!

public MyView(Context context,AttributeSet attrs)

{

super(context,attrs);

mPaint = new Paint();

TypedArray a = context.obtainStyledAttributes(attrs,

R.styleable.MyView);

int textColor = a.getColor(R.styleable.MyView_textColor,

0XFFFFFFFF);

float textSize = a.getDimension(R.styleable.MyView_textSize, 36);

mPaint.setTextSize(textSize);

mPaint.setColor(textColor);

a.recycle();

}

MyView.java MyView控件全部代码如下

public class MyView extends View {

private Paint mPaint;

private Context mContext;

private static final String mString = "Welcome to Mr Wei's blog";

public MyView(Context context) {

super(context);

mPaint = new Paint();

}

public MyView(Context context,AttributeSet attrs)

{

super(context,attrs);

mPaint = new Paint();

TypedArray a = context.obtainStyledAttributes(attrs,

R.styleable.MyView);

int textColor = a.getColor(R.styleable.MyView_textColor,

0XFFFFFFFF);

float textSize = a.getDimension(R.styleable.MyView_textSize, 36);

mPaint.setTextSize(textSize);

mPaint.setColor(textColor);

a.recycle();

}

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

super.onDraw(canvas);

//设置填充

mPaint.setStyle(Style.FILL);

//画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标

canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);

mPaint.setColor(Color.BLUE);

//绘制文字

canvas.drawText(mString, 10, 110, mPaint);

}

}

三、将我们自定义的MyView 加入布局main.xml 文件中,并且使用自定义属性,自定义属性必须加上:
" xmlns:test ="http://schemas.android.com/apk/res/com.android.tutor" ,test是自定义属性的前缀,com.android.tutor是我们包名.
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.android.tutor"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

<com.android.tutor.MyView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

test:textSize="20px"

test:textColor="#fff"

/>

</LinearLayout>

format就是格式,里面的就是这个属性对应的格式,下面列出来大致的格式有:

1. reference:参考某一资源ID,以此类推
(1)属性定义:

(2)属性使用:
<imageview< p="">
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID"
/>
2. color:颜色值

3. boolean:布尔值

4. dimension:尺寸值。注意,这里如果是dp那就会做像素转换

5. float:浮点值。
6. integer:整型值。
7. string:字符串
8. fraction:百分数。
9. enum:枚举值
10. flag:是自己定义的,类似于 android:gravity="top",就是里面对应了自己的属性值。
11. reference|color:颜色的资源文件。 12.reference|boolean:布尔值的资源文件
注意://由于reference是从资源文件中获取:所以在XML文件中写这个属性的时候必须personattr:name="@string/app_name"这种格式,否则会出错
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: