標籤:
在Android布局檔案中,某些時候使用include標籤會很多的好處
1,對於稍微有點複雜的布局介面,將所有布局代碼都寫在一個xml檔案中,介面會顯得很冗餘,可讀性很差,這時可以分開使用include標籤來處理
2,當Activity需要用到同樣的布局效果,也可以使用include標籤處理,而不用把一樣的布局代碼重複拷貝幾遍,不用以後修改起來每個地方都要修改,提高了代碼的重用性
我們先用include標籤實現下面的效果
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="include使用方法" android:gravity="center" /> <include layout="@layout/layout_sec1" android:id="@+id/layout_sec1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/text1" /> <include layout="@layout/layout_sec2" android:id="@+id/layout_sec2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/layout_sec1" /> <include layout="@layout/layout_sec3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /></RelativeLayout>
可以看到在這個布局檔案中使用了3個include標籤對應3個layout,調用了Include之後,對應的細分布局檔案內容就被完全嵌入到了include所指定的位置.這3個layout的代碼就不貼出來了.
此程式只有布局代碼,沒有java代碼,比較簡單,可以自己實現或者下載源碼看下
這裡主要要注意的是,經常有人在RelativeLayout中使用include標籤
但是卻發現include進來的控制項無法用layout_alignParentBottom="true"之類的標籤來調整。其實解決方案非常簡單,只要你在include的時候同時重載下layout_width和layout_height這兩個標籤就可以了。如果不重載,任何針對include的layout調整都是無效的.這個最好親自實驗下就更能體會重載的重要性!
下面看看第二個例子
btn.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://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"> </Button> </LinearLayout> view.xml<!-- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">--> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF0000" /></merge>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <include android:id="@+id/layout1" layout="@layout/btn"/> <include android:id="@+id/layout2" layout="@layout/btn"/> <include layout="@layout/view"/> </LinearLayout>
可以看到在view.xml中用到了merge標籤代替了FrameLayout,因為這裡布局的根標籤是FrameLayout,所以可以使用merge標籤替換,如果根標籤LinearLayout等就不能使用merge了.使用merge的好處是最佳化了UI結構,被匯入的xml用merge作為根節點表示,這樣當被嵌入父級結構中後可以很好的將它所包含的子集融合到父級結構中,而不會出現冗餘的節點,有興趣的朋友可以通過hierarchyViewer工具查看下當前Ui資源的分配情況.
接著看java代碼
package huahua.include2;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;public class MainActivity extends Activity { private LinearLayout layout1 = null; private LinearLayout layout2 = null; private Button btn1; private Button btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); layout1 = (LinearLayout)findViewById(R.id.layout1); layout2 = (LinearLayout)findViewById(R.id.layout2); btn1 = (Button)layout1.findViewById(R.id.btn); btn1.setOnClickListener(new BtnClick()); btn2 = (Button)layout2.findViewById(R.id.btn); btn2.setOnClickListener(new BtnClick()); } private class BtnClick implements View.OnClickListener{ @Override public void onClick(View v) { if(v.equals(btn1)) { btn1.setText("點擊是第一個按鈕"); } else if(v.equals(btn2)) { btn2.setText("點擊是第二個按鈕"); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }}
這時對於相同的R.id.btn要用不同的layout來區別就行了
代碼:這裡
Android include和merge標籤的使用