Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

手機衛士實戰:自定義組合控件 SettingItemView 封裝

編輯:關於 Android 開發

用自定義組合控件簡化設置界面開發

由於設置中心中的各個條目布局非常相似,逐一編寫既費時又難以維護。本文將這些相似布局抽取為自定義組合控件 SettingItemView,從而大幅簡化實現。

自定義組合控件的兩個要點

1. 將已編寫好的布局抽取到一個類中進行管理,下次需要時直接使用組合控件對應的對象;
2. 將組合控件的布局單獨抽取到一個 xml 文件中。

一、新建布局文件 setting_item_view.xml

將上一篇中的條目布局代碼遷移到這裡:

<?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" >
 <RelativeLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:padding="5dp" >
 <TextView
 android:id="@+id/tv_title"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="自動更新設置"
 android:textColor="#000"
 android:textSize="18sp" />
 <TextView
 android:id="@+id/tv_des"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@id/tv_title"
 android:text="自動更新已關閉"
 android:textColor="#000"
 android:textSize="18sp" />
 <CheckBox
 android:id="@+id/cb_box"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_alignParentRight="true"
 android:layout_centerVertical="true" />
 <View
 android:layout_width="match_parent"
 android:layout_height="1dp"
 android:layout_below="@id/tv_des"
 android:background="#000" />
 </RelativeLayout>
</RelativeLayout>

二、通過 SettingItemView 加載布局

新建一個類去加載這段布局文件,並在其中獲取各子控件:

package com.wuyudong.mobilesafe.view;
import com.wuyudong.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;

public class SettingItemView extends RelativeLayout {
 private TextView tv_des;
 private CheckBox cb_box;

 public SettingItemView(Context context) {
 this(context, null);
 }
 public SettingItemView(Context context, AttributeSet attrs) {
 this(context, null, 0);
 }
 public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 // xml --> view,將設置界面的條目轉換成 view 對象
 View.inflate(context, R.layout.setting_item_view, this);
 // 獲取組合控件中的標題描述
 TextView tv_title = (TextView) findViewById(R.id.tv_title);
 tv_des = (TextView) findViewById(R.id.tv_des);
 cb_box = (CheckBox) findViewById(R.id.cb_box);
 }
}

三、簡潔的調用方式

封裝完成後,只需幾行代碼即可完成布局調用:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
 <TextView style="@style/TitleStyle" android:text="設置中心" />
 <com.wuyudong.mobilesafe.view.SettingItemView
 android:layout_width="match_parent"
 android:layout_height="wrap_content" >
 </com.wuyudong.mobilesafe.view.SettingItemView>
</LinearLayout>

運行項目後,效果如下:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved