andriod 获取attrs_android自定义属性attr的方法和类型 | 学步园

本文详细介绍了在Android中如何定义和使用自定义属性,包括reference、Color、boolean、dimension、float、integer、string、fraction、enum、flag等多种类型,并通过实例展示了在自定义View中如何获取和应用这些属性,以及在布局文件中如何声明和使用自定义属性。
摘要由CSDN通过智能技术生成

首先在你需要使用自定义属性的布局文件中定义标签,当然也可以使用系统默认标签android:

xmlns:zkx=http://schemas.android.com/apk/res/com.zkx.test

android:orientation="vertical" android:layout_width="fill_parent";

android:layout_height="wrap_content">

第二行是自定义标签。

格式如上,其中“xmlns:zkx”冒号后面是标签名,在下面使用时(只对当前文件可用)

“com.zkx.test”是你的工程包名。

一、reference:参考指定Theme中资源ID。

1.定义:

2.使用:

二、Color:颜色

1.定义:

2.使用:

三、boolean:布尔值

1.定义:

2.使用:

四、dimension:尺寸值

1.定义:

2.使用:

五、float:浮点型

1.定义:

2.使用:

六、integer:整型

1.定义:

2.使用:

七、string:字符串

1.定义:

2.使用:

八、fraction:百分数

1.定义:

2.使用:

九、enum:枚举

1.定义:

2.使用:

十、flag:位或运算

1.定义:

2.使用:

属性定义时可以指定多种类型值:

使用:

在自定义的View中:

我们在MyView.java 代码修改如下,其中下面的构造方法是重点,我们获取定义的属性我们R.sytleable.MyView_textColor, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ), 防止我们在xml 文件中没有定义.从而使用默认值!

获取,MyView 就是定义在 里的 名字,获取里面属性用名字_ 属性 连接起来就可以.TypedArray 通常最后调用.recycle() 方法,为了保持以后使用该属性一致性!

publicMyView(Context context,AttributeSet attrs)

{

super(context,attrs);

mPaint = newPaint();

TypedArray a = context.obtainStyledAttributes(attrs,

R.styleable.MyView);

inttextColor = a.getColor(R.styleable.MyView_textColor,

0XFFFFFFFF);

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

mPaint.setTextSize(textSize);

mPaint.setColor(textColor);

a.recycle();

}

MyView.java 全部代码如下:

packagecom.android.tutor;

importandroid.content.Context;

importandroid.content.res.TypedArray;

importandroid.graphics.Canvas;

importandroid.graphics.Color;

importandroid.graphics.Paint;

importandroid.graphics.Rect;

importandroid.graphics.Paint.Style;

importandroid.util.AttributeSet;

importandroid.view.View;

publicclassMyViewextendsView {

privatePaint mPaint;

privateContext mContext;

privatestaticfinalString mString ="Welcome to Mr Wei's blog";

publicMyView(Context context) {

super(context);

mPaint = newPaint();

}

publicMyView(Context context,AttributeSet attrs)

{

super(context,attrs);

mPaint = newPaint();

TypedArray a = context.obtainStyledAttributes(attrs,

R.styleable.MyView);

inttextColor = a.getColor(R.styleable.MyView_textColor,

0XFFFFFFFF);

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

mPaint.setTextSize(textSize);

mPaint.setColor(textColor);

a.recycle();

}

@Override

protectedvoidonDraw(Canvas canvas) {

// TODO Auto-generated method stub

super.onDraw(canvas);

//设置填充

mPaint.setStyle(Style.FILL);

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

canvas.drawRect(newRect(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"蓝色是自定义属性的前缀,红色是我们包名.

main.xml 全部代码如下:

version="1.0"encoding="utf-8"?>

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"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/hello"

/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

test:textSize="20px"

test:textColor="#fff"

/>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值