2010-10-06 124 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" />