由浅入深学习自定义控件(1)-给控件添加自定义属性

下面是给已有控件加属性的例子,选择继续已有控件的方式。
       方式一: 第一种方法,直接设置属性值,通过attrs.getAttributeResourceValue拿到这个属性值。
        方式二:通过自定义attrs.xml来实现,在代码中使用context.obtainStyledAttributes获得属性值。

下面是为单选控件加一个value属性的例子

    方式一:这里有可以指定命名空间,不指定传Null也可以

public class MyRadioButton1 extends RadioButton{

private String value;
public static final String NAMESPACE = "http://wei.jiang.selfview";

public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public MyRadioButton1(Context context) {
super(context);
}

public MyRadioButton1(Context context, AttributeSet attrs) {
super(context, attrs);
//命名空间与xml中一致
int resourceId = attrs.getAttributeResourceValue(NAMESPACE, "value", 0);
System.out.println(resourceId);
if (resourceId > 0) {
value = context.getResources().getText(resourceId).toString();
}
System.out.println(value);
}
}

布局文件 :


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:jw="http://wei.jiang.selfview"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >
   
<wei.jiang.selfview.view.MyRadioButton1
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的radioButton1"
        jw:value="@string/myRadiontButton1" />
    
</LinearLayout>

方式二: 先看attrs文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MRadioButton2">
        <attr name="value" format="string" />
    </declare-styleable>
</resources>
这里可以通过typeArray.getString()方法直接获取值,可以先获取resoureId,再取值。
public class MyRadioButton2 extends RadioButton {

 private String value;

 public MyRadioButton2(Context context) {
  super(context);
 }

 public MyRadioButton2(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
 
 }

 public MyRadioButton2(Context context, AttributeSet attrs) {
  super(context, attrs);
  TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MRadioButton2);
  this.value = a.getString(R.styleable.MRadioButton2_value);
  int resourceId = a.getResourceId(R.styleable.MRadioButton2_value, 0);  
  //回收TypedArray,以便后面重用。在调用这个函数后,你就不能再使用这个TypedArray。
  if (resourceId > 0) {
   value = context.getResources().getText(resourceId).toString();
  }
  //value = a.getString(R.styleable.MRadioButton2_value);
  System.out.println(value);
  a.recycle();
 }
 public String getValue() {
  return this.value;
 }

}

布局文件 :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:jw="http://schemas.android.com/apk/res/wei.jiang.selfview"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >
   
    <wei.jiang.selfview.view.MyRadioButton2
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我的radioButton2"
        jw:value="@string/myRadiontButton2" />
    
</LinearLayout>

要注意的是: 一:两种写法中namespace的写法,方式一可以随意指定,方式二则必须以http://schemas.android.com/apk/res/开头
                     二: 布局文件赋值通过资源文件(@string/xx, @drawable/xx), 则控件类中须先取出resourceId,再取值;如果直接给的字符串,则可通过attrs.getAttributeValue()方法与TypeArray.getString()方法直接取值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值