maind.xml檔案
aaaa:duration="yaoyuwen"--->此屬性為自定義屬性
encoding="utf-8"?>
xmlns:aaaa="http://schemas.android.com/apk/res/com.huaqian.attribute" ————>引入自定義屬性的檔案,com.huaqian.attribute為應用程式的檔案包
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
aaaa:duration="yaoyuwen"
/>
自定義一個元件繼承TextView 重寫構造方法
package com.huaqian.attribute;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;
public class MyText extends TextView{
public MyText(Context context, AttributeSet
attrs) {
super(context, attrs);
TypedArray
typedarr=context.obtainStyledAttributes(attrs,R.styleable.MyText);
String
text=typedarr.getString(R.styleable.MyText_duration);
System.out.println("aaaaaaaaaaaaaaa"+text);
setText(text);
}
}
自定義屬性的檔案,該檔案在values檔案夾下
encoding="utf-8"?>
name="MyText">