Android開發之自訂UI組件和屬性

來源:互聯網
上載者:User

Android開發之自訂UI組件和屬性

Android系統雖然內建了很多的組件,但肯定滿足我們個人化的需求,所以我們為了開發方便,需要自訂Android的UI組件,以實現我們個人化的需求。
自訂群組合控制項的步驟:


1 、自定一個View,需要繼承相對布局,線性布局等ViewGroup的子類。ViewGroup是一個其他控制項的容器,能夠乘放各種組件。


2 、實現父類的3個構造方法。一般需要在構造方法裡始化初自訂布局檔案。
一個參數構造方法:為new控制項使用
兩個參數的造方法:在調用布局檔案使用
三個參數的造方法:傳遞帶有樣式的布局檔案使用


3 、根據需求,定義一些API方法。

4 、根據需要自訂控制項的屬性。可以參考TextView的屬性寫。

5 、自訂命名空間。
xmlns:xxx="http://schemas.android.com/apk/res/<包名>" xxx為為scheam名

6 、自訂我們的屬性, 在res/values/attrs.xml(建立屬性檔案)定義屬性

類似:








<

7 、使用自訂的屬性。


例如:
andy:desc_off="設定自動更新已經關閉"
andy:desc_on="設定自動更新已經開啟"
andy:titles="設定自動更新"

8 、在我們自訂控制項的帶兩個參數的構造方法裡面,AttributeSet attrs取出自定屬性值,關聯到自訂布局檔案對應的控制項。

代碼執行個體如下:


1 定義一個自訂控制項:setting_item_view.xml布局檔案


                


2 在對應的Activity布局檔案中調用

   

3 自訂屬性:res/values/attrs.xml


                                        

4 實現自訂群組件,繼承ViewGroup的子類,實現構造方法,和對應的API方法

package com.andy.mobilesafe.ui;import com.andy.mobilesafe.R;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.CheckBox;import android.widget.RelativeLayout;import android.widget.TextView;/** * @author Zhang,Tianyou * @version 2014年11月15日 下午10:22:50 *  *          自訂群組合控制項 兩個TextView 一個checkbox 一個View */public class SettingItemView extends RelativeLayout {private CheckBox cb_status;private TextView tv_title;private TextView tv_desc;private String desc_on;private String desc_off;/** * 初始化布局檔案 *  * @param context */private void initView(Context context) {// 第二個為布局檔案 root第三個參數為布局檔案父類// 把一個布局檔案 View 並載入在SettingItemViewView.inflate(context, R.layout.setting_item_view, this);// View 已經載入該SettingItemViewcb_status = (CheckBox) this.findViewById(R.id.cb_status);tv_desc = (TextView) this.findViewById(R.id.tv_desc);tv_title = (TextView) this.findViewById(R.id.title);}public SettingItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// 這個是可以傳遞一個樣式 調用initView(context);}/** * 帶有兩個參數的構造方法 ,布局檔案使用的時間調用 *  * @param context * @param attrs *            得到屬性值 */public SettingItemView(Context context, AttributeSet attrs) {super(context, attrs);// 自訂布局使用調用的構造方法 attrs為配置的屬性 布局檔案中使用initView(context);String titles = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.andy.mobilesafe","titles");desc_off = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.andy.mobilesafe","desc_off");desc_on = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.andy.mobilesafe","desc_on");tv_title.setText(titles);setDesc(desc_off);}public SettingItemView(Context context) {super(context);// new 出的時間使用initView(context);}/** * 校正群組控制項是否有選中 *  * @return */public boolean isChecked() {return cb_status.isChecked();}/** * 設定群組控制項選中 *  * @param checked */public void setChecked(boolean checked) {if(checked){setDesc(desc_on);}else {setDesc(desc_off);}cb_status.setChecked(checked);}/** * 設定群組控制項的描述資訊 *  * @param text */public void setDesc(String text) {tv_desc.setText(text);}}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.