Android 自定义属性

本文介绍如何在Android中自定义View的属性,包括创建自定义View、定义属性、在布局文件中使用属性及通过TypedArray获取属性值的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.概述

今天看看如何自定义属性,先总结一下步骤:
1.自定义一个View
2.在values文件夹下新建attr.xml文件,在里面定义自己的属性
3.在布局文件中使用自定义的属性,注意命名空间
4.在构造方法中获取自定义的属性

二.实现

下面我们一步步来讲解上面的步骤
1.写出自己的类

public class CustomView extends View {
    public CustomView(Context context) {
        super(context);
    }
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        }
    }
}

2.自定义属性

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

format属性代表当前值得类型:
Android attr.xml文件中attr节点format属性有效值
reference:引用,如其它图片资源
color:颜色
boolean:布尔值
dimension:尺寸值
float:浮点值
integer:整型值
String:字符串
fraction:百分数
enum:枚举值
3.在布局文件中使用自定义的属性
在Android Studio中,使用自定义的属性需要使用命名空间

xmlns:custom="http://schemas.android.com/apk/res-auto"

在eclipse中,需要使用后缀为包名的命名空间

xmlns:custom="http://schemas.android.com/apk/res/com.stone"

布局文件如下:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<com.example.lxn.app1.CustomView
    android:layout_width="wrap_content"
    custom:text = "helloworld"
    custom:textColor = "#ff0000"
    custom:textSize = "20sp"
    android:layout_height="wrap_content" />
</RelativeLayout>

4.在构造方法中获取自定义的属性

我们使用如下的构造方法

  public CustomView(Context context, AttributeSet attrs) {
  }

因为这个构造方法有一个参数attrs,这个参数代表了属性的集合,
我们先把所有的属性打印出来

public class CustomView extends View {
    public CustomView(Context context) {
        super(context);
    }
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        for(int i = 0;i<attrs.getAttributeCount();i++){
            //获得属性的名称
            String name = attrs.getAttributeName(i);
            //获得属性的值
            String value = attrs.getAttributeValue(i);
            System.out.println("=====name"+name+"======"+value);
        }
    }
}

这里写图片描述

我们可以看到把所有的值都正确打印出来了。

下面我们把布局文件改一下,因为在开发中我们可能会写成这样:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<com.example.lxn.app1.CustomView
    android:layout_width="wrap_content"
    custom:text = "@string/helloworld"
    custom:textColor = "#ff0000"
    custom:textSize = "@dimen/height"
    android:layout_height="wrap_content" />
</RelativeLayout>

然后我们继续打印属性的名称和值:

这里写图片描述

我们可以看到,如果我们属性的值采用的是引用的方式,那么打印出来的值也是@的形式,如果我们想要打印出正确的值该怎么办,这时候我们就需要使用TypedArray。

 TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.test);

通过上下文的这个方法来获得 TypedArray对象,该方法接受两个参数,一个是属性集合attr,一个是我们定义的属性集合的名称,通过R.styleable.名称来获得

下面看怎么获得所有的属性:

 public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.test);
        String str = typedArray.getString(R.styleable.test_text);
        int color = typedArray.getColor(R.styleable.test_textColor, -1);
        float size = typedArray.getDimension(R.styleable.test_textSize, 0.1f);
        System.out.println("str="+str+",color="+color+",size="+size);
         typedArray.recycle();
    }

这里写图片描述

我们可以看到,所有的值已经正确获得了。注意,在最后我们要调用recy()方法回收TypedArray,以便下一个调用者进行调用。

下面我们看看attr.xml中的styleable是干什么的,我们可以不使用这个,直接写属性吗,比如

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

答案是可以的。
如果写成这样我们看看需要怎样来获取属性

public class CustomView extends View {
    int[] mAttr = {R.attr.text,R.attr.textSize,R.attr.textColor};
    public CustomView(Context context) {
        super(context);
    }
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,mAttr);
        String str = typedArray.getString(0);
        float size = typedArray.getDimension(1,-1);
        int color = typedArray.getColor(2,-1);
        System.out.println("str="+str+",color="+color+",size="+size);
        typedArray.recycle();
    }
}

这里写图片描述

相比之前,我们将R.styleable.test换成了,mAttr,
R.styleable.text换成了0
R.styleable.textSize换成了1
R.styleable.textColor换成了2
为什么可以这样做呢,其实之前是因为styleable帮我们完成了这些事情,下面我们去R文件中看看就明白了。
这里写图片描述

这里写图片描述
这里写图片描述
这里写图片描述

这下大家明白了吧,所以说styleable帮我们做了一些事情,让我们写起代码来更加方便了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值