編輯:關於 Android 程式設計
一個專案裡經常會需要用到相同的佈局設計。如果每次都複製貼上到同一個 XML 裡,程式碼冗餘、可讀性又差。解法是把共用部分抽成獨立檔案,用 <include> 標籤重複使用。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="@+id/in1"
layout="@layout/btn" />
<include
android:id="@+id/in2"
layout="@layout/btn" />
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
package com.hilary;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* @author hilary
* @Date 2011-12-8
*/
public class TestActivity extends Activity {
private TextView tv = null;
private LinearLayout ll = null;
private LinearLayout ll2 = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);
// 如果一個佈局檔中包含了同一個 XML 檔案,
// 這兩個 XML 中的控制項 Id 是一樣的。
// 當需要操作這些控制項時,需要透過定義這兩個 View 來加以區分。
// 如果只包含同一個 XML 檔案,就不需要這步操作。
ll = (LinearLayout) findViewById(R.id.in1);
ll2 = (LinearLayout) findViewById(R.id.in2);
ll.setBackgroundColor(Color.RED);
Button btn = (Button) ll.findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText("My name is hilary");
}
});
Button btn2 = (Button) ll2.findViewById(R.id.btn);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText("You select second Button!");
}
});
}
}
用 include 重複引入同一個檔案時,被引入的檔案裡所有控制項的 ID 都會重複。上面範例中兩個 btn 的 ID 都是 R.id.btn。
正確做法是透過 include 上的 android:id 先取得外層容器(R.id.in1、R.id.in2),再用該容器的 findViewById() 去找內部的按鈕。如果直接呼叫 Activity 的 findViewById(R.id.btn),永遠只會拿到第一個,第二個按鈕的監聽器根本設不上去——這是 include 最常見的 bug。
被引入的根 View 可以透過 include 標籤覆寫部分屬性:
android:id:覆寫被引入根 View 的 ID,這也是區分多個實例的關鍵。android:layout_*:可以覆寫寬高、邊距等 LayoutParams(但 layout_width 與 layout_height 必須同時指定才會生效,只寫一個會被忽略)。如果所有佈局程式碼都寫在一個 XML 裡,介面會顯得很冗餘、可讀性極差。而且 Android 的佈局是按順序解析的,前面的佈局不能參照其後面佈局的 ID。採用 include 之後,一個 include 中可以參照其後續 include 裡的佈局 ID 屬性,模組之間的依賴關係清楚多了。
如果你的目的只是「消除多餘的巢狀 LinearLayout」,那要用的是 <merge> 而不是 <include>。merge 不會產生額外一層 ViewGroup,會直接把子 View 掛到父容器上,能有效減少 view 層級、提升繪製效能。使用 merge 的前提是它必須搭配 LayoutInflater.inflate() 且 root 不為 null,透過 setContentView() 直接使用是會報錯的。
Android實戰技巧(四十八):在Android上使用Java 8與Kotlin
Java 與 Android 這對搭檔如今也頗有些風雨飄搖的意味。技術圈子裡的事,往往被商業利益牽著鼻子走,世事莫過於此。 一、Java 8 目前 Android 應用開
Android 換膚方案設計:從內置皮膚到與應用分離
換膚是國內應用的常見需求,也是不少App差異化的一部分。本文從功能形態講到技術實現,重點放在「皮膚與應用分離」這套更靈活的方案上。 換膚的三種功能形態 1、內置多套皮
微信好用錢的利息怎麼算?借貸成本試算與風險評估
「好用錢」這類小額借貸平台在使用者間流傳得很快,操作門檻低、撥款速度也快。但在按下借款之前,該弄清楚的不是額度,而是年化成本——這才是真正決定划不划算的數字。實際案例試算
ToolBar 的使用方式與常見設定
ToolBar 是 Android 中取代傳統 ActionBar 的靈活元件。它允許開發者在頂部放入標題、導航圖示、選單與自訂子 View,讓頁面標頭更符合現代設計。