編輯:關於 Android 開發
在做 Android 介面開發時,經常會遇到多個頁面要重複使用同一塊佈局的情況。與其複製粘貼,不如用 <include> 標籤把子佈局引入,既整潔又好維護。本文就用兩個實戰例子,帶大家徹底搞懂 include 單獨使用,以及 include 搭配 merge 的兩種經典玩法。
先看 Activity 的程式碼,重點在於透過 findViewById 先拿到被引入的子佈局,再從子佈局裡定位其中的按鈕,分別綁定不同的事件:
package com.AndroidIncludeLayout;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class AndroidIncludeLayout extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View subLayout1 = (View)findViewById(R.id.main1);
View subLayout2 = (View)findViewById(R.id.main2);
Button myButton_main1 = (Button)subLayout1.findViewById(R.id.mybutton);
Button myButton_main2 = (Button)subLayout2.findViewById(R.id.mybutton);
Button startAnotherActivity = (Button)findViewById(R.id.startanotheractivity);
startAnotherActivity.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setClass(AndroidIncludeLayout.this, AnotherActivity.class);
startActivity(intent);
}});
myButton_main1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
Toast.makeText(AndroidIncludeLayout.this, "Button 1 Pressed", Toast.LENGTH_LONG).show();
}});
myButton_main2.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
Toast.makeText(AndroidIncludeLayout.this, "Button 2 Pressed", Toast.LENGTH_LONG).show();
}});
}
}
主佈局 main.xml 裡,用兩個 <include> 分別引入同一個子佈局,並各自指定不同的 id,這樣才能對它們分別操作:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<include android:id="@+id/main1" layout="@layout/sublayout" />
<include android:id="@+id/main2" layout="@layout/sublayout" />
<Button
android:id="@+id/startanotheractivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Start Another Activity " />
</LinearLayout>
被引入的子佈局 sublayout.xml,這裡是一個帶背景色的線性佈局,裡面放了一個文字和一個按鈕:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#505050" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SubLayout" />
<Button
android:id="@+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=" A Button " />
</LinearLayout>
這種寫法和上面的 include 有點不太一樣,最大的特點是:使用 <merge> 作為子佈局的根節點時,可以直接一步調用 merge 裡面的子元件 id,不必先經過 include 再去找。
Activity 程式碼如下,注意它直接 findViewById(R.id.temperature_light) 就拿到了被 merge 包裹的自訂元件:
package com.hysmarthotel.roomcontrol;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
//燈光控制
public class LightActivity extends Activity {
public static Temperature temp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.light);
temp = (Temperature)findViewById(R.id.temperature_light);
//特殊的地方是,可以直接一步調用 merge 中的 id,不用通過 include 再去調用
init();
}
private void init() {
temp.setTemperature(MainActivity.mTemp + "");
}
}
主佈局 light.xml 採用絕對定位佈局,同樣用 include 引入 time_temp:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg1" >
<TextView
android:id="@+id/lightctrl"
android:layout_x="91.5px"
android:layout_y="93.0px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textSize="42.0px"
android:textColor="#fff3e3d1"
android:text="@string/light_ctrl"
android:drawableLeft="@drawable/ic_light_t"
android:drawablePadding="6px"/>
<include android:id="@+id/include1" layout="@layout/time_temp"/>
</AbsoluteLayout>
而 time_temp.xml 的關鍵,就是以 <merge> 作為根節點。merge 的作用是:引入時不額外多包一層父容器,直接把裡面的元件「灌」進父佈局中,因此元件 id 能被直接使用:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:hyhotel="https://schemas.android.com/apk/res/com.hysmarthotel.roomcontrol" >
<com.hysmarthotel.view.Temperature
android:id="@+id/temperature_light"
android:layout_x="1545.0px"
android:layout_y="109.5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22.5px"
android:textColor="#fff3e3d1"
hyhotel:prefix="@string/room_temp"
hyhotel:unit="@string/celsius" />
</merge>
小結一下:普通 <include> 引入時會保留子佈局的根容器,需要透過 include 的 id 再往下找元件;而搭配 <merge> 時則會「打平」這一層,讓元件 id 直接暴露給父佈局使用,適合用在自訂容器或需要精簡層級的場景。
Android Bitmap 記憶體優化實戰
Android Bitmap 記憶體優化實戰 在 Android 中,圖片顯示是常見需求,但 Bitmap 也是記憶體消耗的大戶。若載入過大或未經最佳化的圖片,容易造成
Kotlin Android 開發指南:建立專案與環境設定
Kotlin Android 開發指南:建立專案與環境設定 Kotlin 是 JetBrains 推出的程式語言,可與 Java 互操作,也越來越常用於 Android
Android 自定義 TitleView:打造可重複使用、樣式彈性的標題欄
Android 自定義 TitleView:打造可重複使用、樣式彈性的標題欄 在 Android 開發過程中,經常會遇到同一個專案裡需要重複定義相同樣式的標題欄。Andr
mitmproxy 實戰:用命令列代理攔截、改寫與重放 HTTP/HTTPS 流量
做 Web 滲透或行動 App 通信分析,HTTP 代理是必備工具。Burp Suite、Fiddler、Charles 幾乎人人都有,但如果你手上只有一台沒有圖形介面的