編輯:關於 Android 開發
上一節完成的組合控件 SettingItemView 靈活性還不夠。這一節,我們仿照系統屬性的做法,為自己的控件定義專屬屬性。
SettingItemView 裡有三個控件:一個顯示大標題的 TextView、一個顯示描述的 TextView,以及一個 CheckBox 復選框。
我們要自定義三個屬性:tsh:title="大標題"、tsh:desc_on="小標題開啟" 與 tsh:desc_off="小標題關閉"。
首先在根佈局加入命名空間:xmlns:tsh="https://schemas.android.com/apk/res/包名"。
接著在 res/values/ 目錄下建立 attrs.xml 檔,加入 <declare-styleable name="TextView"> 節點,並在其下用 <attr name="title" format="string"/> 的方式宣告各個屬性。
使用時,佈局檔會調用帶兩個參數的建構子,其中會傳入一個 AttributeSet 物件。我們有幾種方式從中取值:
呼叫 AttributeSet 的 getAttributeValue() 方法,用索引位置取值(不推薦);
或呼叫 getAttributeValue(namespace, name),傳入命名空間與屬性名,這是我們採用的方式。
取得值後,直接呼叫 setText() 設定標題;描述部分則在 setChecked() 方法裡判斷狀態後再設定對應文案。
SettingItemView.java:
package com.qingguow.mobilesafe.ui; import android.content.Context; import android.util.AttributeSet; import android.widget.CheckBox; import android.widget.RelativeLayout; import android.widget.TextView; public class SettingItemView extends RelativeLayout { private TextView tv_title; private TextView tv_desc; private CheckBox cb_status; private String desc_on; private String desc_off; /** * 初始化View對象 */ private void initView(Context context) { View.inflate(context, R.layout.setting_item_view, this); cb_status=(CheckBox) this.findViewById(R.id.cb_status); tv_desc=(TextView) this.findViewById(R.id.tv_desc); tv_title=(TextView) this.findViewById(R.id.tv_title); } /** * 判斷是否選中 */ public boolean isChecked(){ return cb_status.isChecked(); } /** * 設置是否選中 */ public void setChecked(boolean status){ if(status){ tv_desc.setText(desc_on); }else{ tv_desc.setText(desc_off); } cb_status.setChecked(status); } /** * 設置顯示文本 */ public void setDesc(String text){ tv_desc.setText(text); } public SettingItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(context); } public SettingItemView(Context context, AttributeSet attrs) { super(context, attrs); initView(context); //獲取傳遞的屬性 String title=attrs.getAttributeValue("https://schemas.android.com/apk/res/com.qingguow.mobilesafe", "title"); tv_title.setText(title); desc_on=attrs.getAttributeValue("https://schemas.android.com/apk/res/com.qingguow.mobilesafe", "desc_on"); desc_off=attrs.getAttributeValue("https://schemas.android.com/apk/res/com.qingguow.mobilesafe", "desc_off"); } public SettingItemView(Context context) { super(context); initView(context); } }
activity_setting.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tsh="https://schemas.android.com/apk/res/com.qingguow.mobilesafe" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#ccc" android:gravity="center" android:text="設置中心" android:textSize="20sp" /> <com.qingguow.mobilesafe.ui.SettingItemView tsh:title="設置自動更新" tsh:desc_on="設置自動更新開啟" tsh:desc_off="設置自動更新關閉" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/siv_item"> </com.qingguow.mobilesafe.ui.SettingItemView> </LinearLayout>
attrs.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TextView"> <attr name="title" format="string" /> <attr name="desc_on" format="string" /> <attr name="desc_off" format="string" /> </declare-styleable> </resources>
為 RecyclerView 打造萬能 Adapter,讓列表開發更輕鬆
為 RecyclerView 打造萬能 Adapter,讓列表開發更輕鬆 原文出處:張鴻洋(Granker,@鴻洋_) 一、概述 很久以前我曾針對 ListView 類型
Android 手機衛士實作(十三):系統進程顯示開關與鎖屏一鍵清理
進程列表頁通常有兩個實用設定:一是讓使用者自行決定要不要看到龐雜的系統進程,二是在鎖屏時自動釋放內存。本篇分別說明這兩個功能的實作。 一、顯示/隱藏系統進程 做法是讓
Android Support Library 之 Toolbar 完全指南
Android Support Library 之 Toolbar 完全指南 從 Android 3.0 開始,所有主題預設把 ActionBar 作為 App Bar,
Android 學習指南之四十四:Tab 標籤頁實作教學
Tab 標籤頁控制項在許多程式技術中都見得到,它能讓視窗顯示區被重複利用。在 Android 中,標籤頁會牽涉到 TabActivity、TabHost、TabWidge