Android自定义控件属性

记录下来,方便自己查看。


第一种比较正宗的写法:


1.在values目录下新建attrs.xml文件,在这之中定义属性

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <declare-styleable name="EditTextExt">
        <attr name="Text" format="reference|string"></attr>
        <attr name="Oriental">
            <enum name="Horizontal" value="1"></enum>
            <enum name="Vertical" value="0"></enum>
        </attr>
    </declare-styleable>
</resources>

2.在布局中使用定义的属性,请特别注意空行隔开的行

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"

	xmlns:terry="http://schemas.android.com/apk/res/com.terry.attrs"

	android:orientation="vertical" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">

    <com.terry.attrs.EditTextExt 
	android:id="@+id/ss"
        android:layout_width="fill_parent" 
	android:layout_height="wrap_content"

        terry:Text="fdsafda" 
	terry:Oriental="Vertical"
	
	>
    </com.terry.attrs.EditTextExt>

</LinearLayout>


3.在我们的代码中读取属性并应用

package com.terry.attrs;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class EditTextExt extends LinearLayout {

    public EditTextExt(Context context) {
        this(context, null);
        // TODO Auto-generated constructor stub
    }

    public EditTextExt(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        int resouceId = -1;
        TypedArray typeArray = context.obtainStyledAttributes(attrs,
                R.styleable.EditTextExt);

        TextView tv = new TextView(context);
        EditText et = new EditText(context);
        
        int N = typeArray.getIndexCount();
        for (int i = 0; i < N; i++) {
            int attr = typeArray.getIndex(i);
            switch (attr) {
            case R.styleable.EditTextExt_Oriental:
                resouceId = typeArray.getInt(R.styleable.EditTextExt_Oriental,
                        0);
                this.setOrientation(resouceId == 1 ? LinearLayout.HORIZONTAL
                        : LinearLayout.VERTICAL);
                break;
            case R.styleable.EditTextExt_Text:
                resouceId = typeArray.getResourceId(
                        R.styleable.EditTextExt_Text, 0);
                tv.setText(resouceId > 0 ? typeArray.getResources().getText(
                        resouceId) : typeArray
                        .getString(R.styleable.EditTextExt_Text));
                break;
            }
        }
        addView(tv);
        addView(et);
        typeArray.recycle();
    }
}


第二种方法:

1.布局文件中直接写属性名称加引用,注意空行隔开的行

<com.terry.attrs.EditTextExt1 
	android:id="@+id/ss3"
        android:layout_width="wrap_content" 
	android:layout_height="wrap_content"

	Text="@string/app_name" >

</com.terry.attrs.EditTextExt1>

2.在代码中获取属性值

package com.terry.attrs;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class EditTextExt1 extends LinearLayout {

    private String Text = "";

    public EditTextExt1(Context context) {
        this(context, null);
        // TODO Auto-generated constructor stub
    }

    public EditTextExt1(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        int resouceId = -1;

        TextView tv = new TextView(context); 
        EditText et = new EditText(context);

        resouceId = attrs.getAttributeResourceValue(null, "Text", 0);
        if (resouceId > 0) {
            Text = context.getResources().getText(resouceId).toString();
        } else {
            Text = "";
        }
        tv.setText(Text);

        addView(tv);
        addView(et, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        this.setGravity(LinearLayout.VERTICAL);
    }
}


相比较而言

  • 第二种可以编译时报错,如果编程人员随便输入什么第一种是不会报错的,第二种可以支持代码检测功能。
  • 第二种写法,跟Android 属性标准写法是一致的,而且可以统一书法规则。
  • 第二种写法,可以支持数据格式的验证,比如我们在attrs上注明只支持integer 那么就不可以使用字符串,这是第一种达不到的。
  • 第二种写法,可以为VIEW提供选择操作,比如如上我们使用的ENUM让VIEW对应的属性支持ENUM列表,或者为其提供BOOL等只有双项选择的操作。
  • 第一种写法,所有的属性必须是引用自资源(不大确定,如果朋友有什么好的DEMO麻烦共享),第二种写法,可以即支持引用资源又可以直接输入做操作,为编程带来更多的方便性。

种种都说明,第二种写法更具规范性,功能更性,代码编写 也更优雅。


原文地址:http://blog.csdn.net/jincf2011/article/details/6344678

自己做了下整理,留作参考资料。




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值