android自定義控制元件(五) 自定義組合控制元件
阿新 • • 發佈:2019-02-16
2.自定義控制元件程式碼,從LinearLayout繼承:< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical"> < TextView android:layout_height="wrap_content" android:id="@+id/text1" android:layout_width="wrap_content">< /TextView> < ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btn1">< /ImageButton> < /LinearLayout>
Java程式碼
public class ImageBtnWithText extends LinearLayout { } public ImageBtnWithText(Context context) { this(context, null); } public ImageBtnWithText(Context context, AttributeSet attrs) { super(context, attrs); //在建構函式中將Xml中定義的佈局解析出來。 LayoutInflater.from(context).inflate(R.layout.imagebtn_with_text, this, true); } }
3.在主介面佈局xml中使用自定義控制元件:
Xml程式碼
< com.demo.widget2.ImageBtnWithText
android:id="@+id/widget"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
即使用完整的自定義控制元件類路徑:com.demo.widget2.ImageBtnWithText定義一個元素。
執行可以看到控制元件已經能夠被載入到介面上。
4.給按鈕設定圖片和文字
如果圖片是固定不變的,那麼直接在控制元件佈局中設定ImageButton的src屬性即可。
4.1通過Java程式碼設定,在控制元件程式碼中提供函式介面:
public void setButtonImageResource(int resId) {
mBtn.setImageResource(resId);
}
public void setTextViewText(String text) {
mTv.setText(text);
}
然後再在主介面的onCreate()通過函式呼叫設定即可。
4.2通過Xml設定屬性
4.2.1首先定義Xml可以設定的屬性集合,在values下建立attrs.xml,檔名可隨意,一般都叫attrs.xml
Xml程式碼
< ?xml version="1.0" encoding="utf-8"?>
< resources>
< declare-styleable name="ImageBtnWithText">
< attr name="android:text"/>
< attr name="android:src"/>
< /declare-styleable>
< /resources>
屬性集合名字:ImageBtnWithText,自己可根據實際來定義;
集合中包含的屬性列表,每行一個屬性。
4.2.2修改自定義控制元件實現程式碼,以獲取xml中定義的屬性
Java程式碼
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ImageBtnWithText);
CharSequence text = a.getText(R.styleable.ImageBtnWithText_android_text);
if(text != null) mTv.setText(text);
Drawable drawable = a.getDrawable(R.styleable.ImageBtnWithText_android_src);
if(drawable != null) mBtn.setImageDrawable(drawable);
a.recycle();
首先通過context.obtainStyledAttributes獲得所有屬性陣列;
然後通過TypedArray類的getXXXX()系列介面獲得相應的值。
4.2.3在主介面佈局中設定自定義控制元件的屬
android:text="ABC" android:src="@drawable/icon
4.3自定義名稱屬性:
在4.2中使用的屬性名是Android系統名稱空間的,都以android開頭,比如android:text等。
實際開發中會自定義一些屬性名,這些屬性名仍然定義在4.2.1提到的attrs.xml中:
4.3.1定義屬性名
Xml程式碼
< attr name="appendText" format="string"/>
和直接使用系統的attr不同的是要增加一個format屬性,說明此屬性是什麼格式的。format可選項可參見注1
4.3.2使用自定義屬性
Xml程式碼
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myspace="http://schemas.android.com/apk/res/com.demo.customwidget"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
< com.demo.widget2.ImageBtnWithText
android:text="ABC" android:src="@drawable/icon" android:id="@+id/widget"
android:layout_width="fill_parent" android:layout_gravity="center"
android:layout_height="fill_parent" myspace:appendText="123456">
< /com.demo.widget2.ImageBtnWithText>
< /LinearLayout>
效果圖