編輯:關於 Android 程式設計
公司網路實在堪憂,插件市場時好時壞;而每次都要手打一堆 findViewById 也確實枯燥。於是我就自己寫了個小工具:讀入布局檔,自動產生對應的宣告與綁定程式碼,並直接複製到剪貼簿。
以下是一個典型的「兒童保護」設定頁,包含標題列、四個輸入欄位與一顆確認鈕:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/home_backgroud"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RelativeLayout
android:id="@+id/headerLine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#3d61f3"
android:orientation="horizontal">
<TextView
android:id="@+id/title"
style="@style/header"
android:text="兒童保護" />
<ImageView
android:id="@+id/setchild_menu_btn"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:src="@drawable/ic_back" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:orientation="vertical">
<!-- 開始時間 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="開始時間"
android:gravity="center_vertical" />
<EditText
android:id="@+id/et_childstarttime"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:singleLine="true" />
</LinearLayout>
<!-- 結束時間 -->
<LinearLayout ... >
<EditText
android:id="@+id/et_childendtime"
android:singleLine="true" />
</LinearLayout>
<!-- 性別 -->
<LinearLayout ... >
<EditText
android:id="@+id/et_childsex"
android:hint="男/女"
android:singleLine="true" />
</LinearLayout>
<!-- 週期 -->
<LinearLayout ... >
<EditText
android:id="@+id/et_childperiod"
android:singleLine="true" />
</LinearLayout>
<LinearLayout ... >
<Button
android:id="@+id/setchild_ok"
android:text="確認" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
工具掃過所有帶 id 的節點,依型別產生成員變數宣告,再補上一個 bindview() 方法把對應關係一次綁好:
private RelativeLayout headerLine;
private TextView title;
private ImageView setchild_menu_btn;
private EditText et_childstarttime;
private EditText et_childendtime;
private EditText et_childsex;
private EditText et_childperiod;
private Button setchild_ok;
private void bindview() {
headerLine = (RelativeLayout) findViewById(R.id.headerLine);
title = (TextView) findViewById(R.id.title);
setchild_menu_btn = (ImageView) findViewById(R.id.setchild_menu_btn);
et_childstarttime = (EditText) findViewById(R.id.et_childstarttime);
et_childendtime = (EditText) findViewById(R.id.et_childendtime);
et_childsex = (EditText) findViewById(R.id.et_childsex);
et_childperiod = (EditText) findViewById(R.id.et_childperiod);
setchild_ok = (Button) findViewById(R.id.setchild_ok);
}
實際上,如今這類需求已有 ButterKnife、Android KTX 擴充函式等成熟方案可用,不必自己造輪子。但從這個小練習能釐清一個觀念:findViewById 本質上就是依 id 在檢視樹中查找節點,型別由布局檔決定。理解這一層之後,再回頭看那些「自動化」的庫,就會發現它們做的也不過是把同樣的查找與轉型,提前到編譯期或透過產生程式碼完成而已。
Android 開發實戰:精準取得螢幕、狀態列與標題列高度
為什麼「取高度」在 Android 裡不直覺 在 Android 上做自適應版面時,經常需要知道螢幕有多高、狀態列佔了多少、標題列又吃掉多少像素。這些數值分散在不同 AP
Android 自訂進度條:從系統源碼學會客製化 ProgressBar
1. 成品預覽 2. 準備素材 progress1.png(78*78)、progress2.png(78*78) 3. 實作原理 一張圖當背景(通常選淺色),另
Android 屬性動畫入門(二):從原理到 API 上手
上一篇聊完了逐幀動畫與補間動畫,這篇把重心放到屬性動畫上。它是目前最靈活、也最常用的動畫方案,可配置的項目包括: Duration:動畫總時長,預設 300ms; Ti
Android windowSoftInputMode 屬性完整詳解
在先前的文章中,我們曾簡單介紹過如何讓軟鍵盤不自動彈出,方法是設定 android:windowSoftInputMode 屬性。那麼這個屬性究竟是做什麼用的、又有哪些作