天天看點

Android 自定義控件——View(學習篇)

自定義View步驟:

1、自定義View的屬性;

2、在構造方法中擷取自己定義的屬性;

3、重寫onMesure();

4、重寫onDraw();

學習小結:

一、declare-styleable:declare-styleable是給自定義控件添加自定義屬性用的。

1.首先,先寫attrs.xml

在res-vlaues檔案夾下建立資源檔案attrs.xml或則自定義一個資源檔案xx.xml,都可以。

之後在裡面配置declare-styleable ,name為PersonAttr。

可能這裡有人會問,format是什麼,裡面的單詞代表的又是什麼意思。

format就是格式,裡面的就是這個屬性對應的格式,下面列出來大緻的格式有:

1. reference:參考某一資源ID,以此類推

(1)屬性定義:

<declare-styleable name = "名稱">

<attr name = "background" format = "reference" />

</declare-styleable>

(2)屬性使用:

<ImageView

android:layout_width = "42dip"

android:layout_height = "42dip"

android:background = "@drawable/圖檔ID"

/>

2. color:顔色值

<declare-styleable name = "名稱">

<attr name = "textColor" format = "color" />

</declare-styleable>

3. boolean:布爾值

<declare-styleable name = "名稱">

<attr name = "focusable" format = "boolean" />

</declare-styleable>

4. dimension:尺寸值。注意,這裡如果是dp那就會做像素轉換

<declare-styleable name = "名稱">

<attr name = "layout_width" format = "dimension" />

</declare-styleable>

5. float:浮點值。

6. integer:整型值。

7. string:字元串

8. fraction:百分數。

9. enum:枚舉值

10. flag:是自己定義的,類似于 android:gravity="top",就是裡面對應了自己的屬性值。

11. reference|color:顔色的資源檔案。

12.reference|boolean:布爾值的資源檔案

注意://由于reference是從資源檔案中擷取:是以在XML檔案中寫這個屬性的時候必須 personattr:name="@string/app_name"這種格式,否則會出錯

2.設定好屬性檔案後,在使用的布局中寫相關配置:

  1. xmlns:personattr="http://schemas.android.com/apk/res/com.example.declare_styleable"  
  2. xmlns:你自己定義的名稱="http://schemas.android.com/apk/res/你程式的package包名"    (我這是com.example.declare_styleable)  

3.最後在自定義控件的構造方法中擷取你配置的屬性值:

public class PersonView extends TextView {
	public PersonView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public PersonView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public PersonView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		TypedArray tArray = context.obtainStyledAttributes(attrs,R.styleable.PersonAttr);//擷取配置屬性
		String name = tArray.getString(R.styleable.PersonAttr_name);<span style="font-family: Arial, Helvetica, sans-serif;">//得到屬性name</span>
		int age = tArray.getInt(R.styleable.PersonAttr_age, 15);
		Boolean adult = tArray.getBoolean(R.styleable.PersonAttr_adult, false);
		String str_adult = getAdultStatus(adult);
		int weight = tArray.getInt(R.styleable.PersonAttr_weight, 1);// 預設是中等身材,屬性為:1
		String str_weight = getWeightStatus(weight);//獲得肥胖屬性
		float textSize = tArray.getDimension(R.styleable.PersonAttr_textSize,R.dimen.default_text_size);// 如果你設定為DP等機關,會做像素轉換
		tArray.recycle();//回收資源
//		setTextSize(textSize);//設定字型大小
		setText("姓名:" + name + "\n" + "年齡:" + age + "\n" + "是否成年:" + str_adult
				+ "\n" + "體形:" + str_weight);//給自定義的控件指派
	}
	
	/** 根據傳入的值判斷是否成年 */
	public String getAdultStatus(Boolean adult ){
		String str_adult = "未成年";
		if (adult) {
			str_adult = "成年";
		}
		return str_adult;
	}
	
	/** 根據傳入的值判斷肥胖狀态 */
	public String getWeightStatus(int weight){
		String str_weight = "中等";
		switch (weight) {
		case 0:
			str_weight = "瘦";
			break;
		case 1:
			str_weight = "中等";
			break;
		case 2:
			str_weight = "肥胖";
			break;
		default:
			break;
		}
		return str_weight;
	}
}