編輯:關於 Android 程式設計
整理日常筆記時,我決定把「自定義組合控制項」這件事從頭寫一遍。這個主題說起來並不玄妙,卻是很多入門開發者第一次感覺到「框架給我的積木不夠用」的地方。
先談談動機。系統原生控制項能覆蓋的場景终归有限,當設計稿上出現「左邊一行標題、右邊一個開關、背景還要隨位置變化」這種反覆使用的樣式時,複製粘貼布局顯然不是長久之計。此時把若干基礎控制項封裝成一個整體,並通過自定義屬性對外暴露配置入口,才是更合理的做法。
第一步:先寫一份布局。這份布局就是組合控制項的「骨架」,本例使用 RelativeLayout 作為根節點。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="設置自動更新"
android:textColor="#000000"
android:textSize="16sp" />
<ImageView
android:id="@+id/iv_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/off" />
</RelativeLayout>
第二步:寫一個類去繼承根布局。這裡刻意繼承 RelativeLayout 而非 FrameLayout,原因是後續可能會直接用到根容器自身的屬性與能力。
public class SettingItemView extends RelativeLayout { }
第三步:處理構造方法。自定義 View 有三個構造方法,一參、二參、三參。系統在 inflate 時只會調用前兩個,三參版本不會被自動觸發,因此需要由前兩個構造方法顯式轉調。把初始化邏輯集中在三參構造裡,是比較省心的組織方式。
// 系統不會主動調用這個構造,需要由另外兩個構造轉調
public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SettingItemView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SettingItemView(Context context) {
this(context, null);
}
public class SettingItemView extends RelativeLayout {
public static final String NAMESPACE = "https://schemas.android.com/apk/com.xxx.myphonesafe";
private View view;
private TextView tv_title;
private ImageView iv_toggle;
boolean isToggle = false; // 記錄當前開關狀態
public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// 讀取布局中為自定義控制項配置的屬性
// 參數1:命名空間;參數2:屬性名;參數3:取不到時的預設值
int background = attrs.getAttributeIntValue(NAMESPACE, "backgroundres", 0);
String title = attrs.getAttributeValue(NAMESPACE, "title");
boolean isShowToggle = attrs.getAttributeBooleanValue(NAMESPACE, "isshowtoggle", true);
initView();
tv_title.setText(title);
iv_toggle.setVisibility(isShowToggle ? View.VISIBLE : View.INVISIBLE);
setBackGround(background);
}
private void setBackGround(int background) {
switch (background) {
case 0: // 首項
setBackgroundResource(R.drawable.first_normal);
break;
case 1: // 中間項
setBackgroundResource(R.drawable.middle_normal);
break;
case 2: // 末項
setBackgroundResource(R.drawable.last_normal);
break;
}
}
public SettingItemView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SettingItemView(Context context) {
this(context, null);
}
private void initView() {
view = View.inflate(getContext(), R.layout.setting_item_view, null);
tv_title = (TextView) view.findViewById(R.id.tv_title);
iv_toggle = (ImageView) view.findViewById(R.id.iv_toggle);
addView(view);
}
public boolean isToggle() {
return isToggle;
}
public void setToggle(boolean toggle) {
isToggle = toggle;
iv_toggle.setImageResource(toggle ? R.drawable.on : R.drawable.off);
}
}
在 res/values 下新建 attrs.xml(文件名稱建議就用 attrs),用 declare-styleable 標簽聲明:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SettingItemView">
<!-- 控制顯示的背景圖 -->
<attr name="backgroundres" format="enum">
<enum name="first" value="0" />
<enum name="middle" value="1" />
<enum name="last" value="2" />
</attr>
<!-- 控制顯示的文字 -->
<attr name="title" format="string" />
<!-- 控制是否顯示開關 -->
<attr name="isshowtoggle" format="boolean" />
</declare-styleable>
</resources>
注意兩處容易踩坑的細節:標簽是 value 而不是 values,元素是 string 而不是 String。
由於用到了自定義屬性,使用方的布局必須先聲明命名空間,且該命名空間要與控制項代碼中的常量保持一致。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:abc="https://schemas.android.com/apk/com.xxx.myphonesafe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="設置中心" />
<com.xxx.myphonesafe.ui.SettingItemView
android:id="@+id/siv_auto_update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/color_selector"
abc:backgroundres="first"
abc:isshowtoggle="true"
abc:title="設置是否自動更新" />
<com.xxx.myphonesafe.ui.SettingItemView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/color_selector"
abc:backgroundres="last"
abc:isshowtoggle="true"
abc:title="設置攔截騷擾" />
</LinearLayout>
關於命名空間前綴 abc:它可以任意更名,但同一文件內引用屬性時必須保持一致。而 com.xxx.myphonesafe 這部分對應清單文件中的包名,不可隨意改動,否則屬性會解析失敗。
最後新建一個 Activity,把 setContentView 指向這份布局即可看到效果。整套流程的關鍵只有一句話:布局先行,代碼封裝,屬性對外開放。
Android 官方文件翻譯:Working with System Permissions(與系統權限共事)
Working with System Permissions 為了保護系統完整性與使用者隱私,Android 會將每個 App 執行在一個受限的沙盒中。若 App 想使
TraceView 使用全解:用圖形化視角揪出 Android 效能瓶頸
TraceView 是 Android 平台自帶的效能分析利器。它把方法級別的執行資訊以圖形方式鋪開,讓我們能直觀地看出時間究竟花在哪裡,而不是靠猜。 TraceView
ViewDragHelper 側滑欄實戰:拖拽、邊界與釋放回調全解析
在實現側滑欄、拖拽排序、懸浮菜單等效果時,ViewDragHelper 是一個非常實用的工具類。它能幫助我們處理觸摸攔截、子 View 拖拽、邊界限制與釋放後的吸附動畫,
Retrofit 2.0 文件下載進度顯示實作指南
Retrofit 2.0 本身沒有內建下載進度回調,但它建立在 OkHttp 之上,因此可以透過攔截器與自定義 ResponseBody 來補足這個能力。自定義進度監聽先