編輯:關於 Android 程式設計
本文以實例說明如何在 Android 中透過 include 呼叫內部元件,供需要的朋友參考,具體如下。
例子一:
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> main.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="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>
那麼該如何呼叫被 include 進來的元件呢?
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 { /** Called when the activity is first created. */ @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) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(AndroidIncludeLayout.this, AnotherActivity.class); startActivity(intent); }}); myButton_main1.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(AndroidIncludeLayout.this, "Button 1 Pressed", Toast.LENGTH_LONG).show(); }}); myButton_main2.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub Toast.makeText(AndroidIncludeLayout.this, "Button 2 Pressed", Toast.LENGTH_LONG).show(); }}); } }
但如果被 include 進來的 xml 改成這樣:
sublayout.xml
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="https://schemas.android.com/apk/res/android"> <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 " /> </merge>
那麼上面的方法就行不通了,會報空指標。這是因為用了 merge 之後,導入進來就相當於是當前 view 底下的元件了,所以直接 findViewById 即可。這樣一來,也就能解決重複 include 同一個 layout 的問題。
希望本文所述能對大家的 Android 開發有所幫助。
Android 持久化技術之文件讀寫實例詳解
本文通過實例分析 Android 持久化技術中的文件讀取與寫入操作,供感興趣的讀者參考。 1、文件存儲要點 (1)在 Android 的持久化技術中,文件存儲是最基本的一
Android 模擬器開啟 GPU 加速:原理、踩坑與虛擬機環境實測
模擬器的 3D 加速架構 Android 模擬器有個 GPU 開關。打開之後,3D 加速的工作會轉交給宿主機(HOST)的 GPU;不開的話就退回 Soft 3D,也就是
Android 四大組件之 Activity(六):狀態保存與恢復的真相
這一節要談的是 Activity 的狀態保存與恢復。這個主題單看條文很枯燥,動手做兩組對照實驗就一清二楚了。 準備一個實驗用的 Activity 布局放兩個按鈕和一個輸入
Android 多媒體:人臉識別(FaceDetector)
1. 相關背景 Google 於 2006 年收購 Neven Vision 公司(該公司擁有十多項應用於行動裝置的圖像識別專利),藉此獲得圖像識別技術,並很快應用於 P