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

高仿知乎實作筆記(一):自訂控制元件的封裝與使用

編輯:關於 Android 程式設計

先來看實作後的效果:

從「我的關注」往下,每一列其實都是同一個自訂組合控制元件。這篇就把它從零開始拆解一次。

一、依設計稿完成版面配置

這個頁面的自訂元件中,「夜間模式」最複雜——它在右側多了一顆 Switch。既然它是 superset,就拿它當成基礎版面。

結構是:左側圖示、中間文字、底部細分隔線,右側一個預設隱藏的 Switch。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/selector_msi">

    <ImageView
        android:id="@+id/iv_more_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:paddingLeft="15dp"
        android:src="@drawable/ic_remove_red_eye_white_18dp"
        android:tint="@android:color/darker_gray" />

    <TextView
        android:id="@+id/tv_moretext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/iv_more_icon"
        android:paddingLeft="15dp"
        android:text="我的關注"
        android:textSize="16sp" />

    <ImageView
        android:id="@+id/iv_more_line"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_alignLeft="@+id/tv_moretext"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="15dp"
        android:background="@drawable/list_divider"
        android:tint="@android:color/darker_gray" />

    <Switch
        android:id="@+id/switch_more"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:paddingRight="10dp"
        android:visibility="invisible" />
</RelativeLayout>

二、宣告自訂屬性

在 res/values 下新增 attrs.xml,用來定義這個元件對外開放的屬性。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MorePersonalSettingItem">
        <!-- 條目文字 -->
        <attr name="moretext" format="string" />
        <!-- 是否顯示底部橫線 -->
        <attr name="isShowLine" format="boolean" />
        <!-- 條目圖示 -->
        <attr name="moreicon" format="reference" />
        <!-- 是否顯示右側按鈕 -->
        <attr name="isShowToggle" format="boolean" />
    </declare-styleable>
</resources>

三、建立自訂元件類別

光有屬性宣告還不夠,得在程式碼裡把它們讀出來並套用到檢視上。因為基礎版面是 RelativeLayout,就讓自訂類別繼承它(繼承其他 Layout 當然也可以)。

public class MorePersonalSettingItem extends RelativeLayout {

    private static final String NAMESPACE = "https://schemas.android.com/apk/res-auto";

    private ImageView iv_more_icon;
    private ImageView iv_more_line;
    private TextView tv_moretext;
    private Switch switch_more;

    public MorePersonalSettingItem(Context context) {
        this(context, null);
    }

    public MorePersonalSettingItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MorePersonalSettingItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        // 讀取 XML 中宣告的屬性
        String moreText = attrs.getAttributeValue(NAMESPACE, "moretext");
        Boolean isShowLine = attrs.getAttributeBooleanValue(NAMESPACE, "isShowLine", true);
        int moreicon = attrs.getAttributeResourceValue(NAMESPACE, "moreicon", R.drawable.ic_live_tiny);
        Boolean isShowToggle = attrs.getAttributeBooleanValue(NAMESPACE, "isShowToggle", false);

        initView();

        iv_more_icon.setImageResource(moreicon);
        tv_moretext.setText(moreText);

        iv_more_line.setVisibility(isShowLine ? View.VISIBLE : View.INVISIBLE);
        switch_more.setVisibility(isShowToggle ? View.VISIBLE : View.INVISIBLE);
    }

    private void initView() {
        // 載入版面並掛到自身
        View view = View.inflate(getContext(), R.layout.more_setting_item, null);
        iv_more_icon = (ImageView) view.findViewById(R.id.iv_more_icon);
        iv_more_line = (ImageView) view.findViewById(R.id.iv_more_line);
        tv_moretext = (TextView) view.findViewById(R.id.tv_moretext);
        switch_more = (Switch) view.findViewById(R.id.switch_more);
        addView(view);
    }
}

四、在版面中使用

使用時有兩個容易踩到的地雷:一是標籤名稱必須寫完整套件名稱;二是外層版面必須宣告 xmlns:app 命名空間,否則編譯時找不到對應屬性而報錯。

xmlns:app="https://schemas.android.com/apk/res-auto"

使用範例:

<!-- 個人設定區塊 -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="6dp"
    android:orientation="vertical">

    <com.example.zhihu10.ui.MorePersonalSettingItem
        android:id="@+id/msi_my_concern"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:isShowLine="true"
        app:moreicon="@drawable/ic_follow_more"
        app:moretext="我的關注" />

    <com.example.zhihu10.ui.MorePersonalSettingItem
        android:id="@+id/msi_my_collect"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:isShowLine="true"
        app:moreicon="@drawable/ic_grade"
        app:moretext="我的收藏" />

    <com.example.zhihu10.ui.MorePersonalSettingItem
        android:id="@+id/msi_my_live"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:isShowLine="false"
        app:moreicon="@drawable/ic_feed_livebanner_logo"
        app:moretext="我的Live" />
</LinearLayout>

<!-- 屬性設定區塊 -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="15dp"
    android:orientation="vertical">

    <com.example.zhihu10.ui.MorePersonalSettingItem
        android:id="@+id/msi_night_mode"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:isShowLine="true"
        app:isShowToggle="true"
        app:moreicon="@drawable/ic_nightmode_more"
        app:moretext="夜間模式" />

    <com.example.zhihu10.ui.MorePersonalSettingItem
        android:id="@+id/msi_my_setting"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:isShowLine="false"
        app:moreicon="@drawable/ic_drawer_settings"
        app:moretext="設置" />
</LinearLayout>

可以看到,同一個元件透過屬性組合,就覆蓋了「有沒有分隔線」、「有沒有 Switch」等多種形態,不必為每一列各寫一份版面。

這個例子當然很簡單,但自訂元件的價值正在於此:把重複的結構封裝起來,讓版面檔從一堆 XML 折疊成幾行標籤。它也能補足系統內建控制元件做不到的效果,進而實作出更複雜的版面與更精緻的視覺。更進階的用法(例如自訂 onMeasure / onDraw、屬性動畫、樣式主題)留待後續實際案例再談。

專案 GitHub 位址:https://github.com/WanXuJiao/ZhiHu1.0

專案仍在持續開發中,目前已完成整體架構與版面,也歡迎各路高手一起來改進。

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