记录下来,方便自己查看。
第一种比较正宗的写法:
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>
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>
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); } }
相比较而言
种种都说明,第二种写法更具规范性,功能更性,代码编写 也更优雅。
原文地址:http://blog.csdn.net/jincf2011/article/details/6344678。
自己做了下整理,留作参考资料。