編輯:關於 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
專案仍在持續開發中,目前已完成整體架構與版面,也歡迎各路高手一起來改進。
貝塞爾曲線開發的藝術:從原理到動畫實戰
一句話概括貝塞爾曲線:把任意一條平滑曲線,轉化為精確可控的數學公式。 我們在眾多繪圖工具中常見的鋼筆工具,正是貝塞爾曲線的經典應用。 貝塞爾曲線有幾個關鍵名詞:起始點、
Android 基礎(二十四):EditText 輸入框完全解析
介紹 文字輸入框(Text Field)讓使用者能將文字輸入你的 App,它可以是單行,也可以是多行。觸碰文字輸入框時,游標會自動定位並彈出鍵盤。除了輸入文字之外,輸入框
Android ImageView 圖片顯示使用方法
加入圖片資源在 Android 中顯示圖片,建議將圖片放入 res/drawable 開頭的資源資料夾。系統會依螢幕解析度選擇合適的圖片,避免自行以檔案流讀取造成管理困難
Android手機衛士密碼保存:MD5加密實作與安全提醒
為什麼保存密碼時需要加密 一般使用者的手機若沒有Root權限,通常無法直接進入/data/data目錄查看應用程式內部資料。但如果裝置取得Root權限,或經過刷機、系統改