2010-10-06 121 views

回答

5

是,你可以在XML做到這一點。見android docs 有關合並/包括

基本上你將有1個佈局(root.xml)如下:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rootLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
    android:id="@+id/headingLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
     <include 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     layout="@layout/heading_layout" /> 
    </LinearLayout> 
<RelativeLayout> 

heading_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<merge 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 
     <ImageView 
      android:id="@+id/titleImg" 
      android:src="@drawable/bg_cell" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <TextView 
      android:id="@+id/titleTxt" 
      android:layout_centerInParent="true" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
    </RelativeLayout> 
</merge> 

所以你Activity你會setContentView(R.layout.root);這將包括標題。

,你也可以做一些很酷的東西除了這個程序,如XML插入佈局到了root.xml(後setContentView();

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
inflater.inflate(R.layout.home, rootLayout); 

其中rootLayout是由ID和R.layout.home發現父RelativeLayout是您希望添加到根的佈局

0

當然可以。退房this後進行了詳細的解釋:

<include android:id="@+id/my_id" layout="@layout/layout_id" />