Androidカスタムコントロール属性


記録して、自分で見るのが便利です.
1つ目の本格的な書き方:
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();
    }
}

2つ目の方法:
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);
    }
}

比較すると
  • 第2種は時報エラーをコンパイルすることができ、プログラマが勝手に何かを入力すれば、第1種はエラーを報告しないが、第2種はコード検出機能をサポートすることができる.
  • の2番目の書き方は、Android属性標準の書き方と一致し、書道のルールを統一することができます.
  • の2番目の書き方は、データフォーマットの検証をサポートすることができます.例えば、integerのみをサポートすると文字列を使用できないことをattrsに明記すると、1番目には達成できません.
  • の2つ目の書き方は、上記のように私たちが使用しているENUMがVIEWに対応する属性にENUMリストをサポートさせたり、BOOLを提供したりする2つの選択のみの操作など、VIEWに選択操作を提供することができる.
  • 第1の書き方は、すべての属性がリソースから引用されなければならない(友人が何か良いDEMOを持っている場合は共有しなければならない)、第2の書き方は、参照リソースをサポートし、直接入力して操作することができ、プログラミングにより多くの利便性をもたらすことができる.

  • いろいろなことが説明されていますが、第2の書き方はもっと規範性があり、機能がもっと性があり、コードの作成ももっと優雅です.
    原文住所:http://blog.csdn.net/jincf2011/article/details/6344678.
    自分で整理して、参考資料に残しておきます.