在另一个布局中添加布局

时间:2011-01-25 09:35:57

标签: android android-layout

如何在另一个layout中添加一个布局。我在tblrow.XML.so中创建了这个布局。我想在menu.XML.i中添加这些行,想要根据行数来添加这些行。我该怎么办?如果我添加它,我如何识别每一行。请帮助我。我需要java中的解决方案而不是XML。 我的代码是

<TableRow android:id="@+id/tblRowMovies" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/btn_backgrnd"  android:clickable="true" android:focusable = "true" android:layout_weight="1.0">
<ImageView android:layout_width="wrap_content" android:layout_height="fill_parent" android:src="@drawable/movie" android:layout_gravity="center_vertical|center_horizontal"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="MOVIES" android:layout_gravity="center_vertical" android:paddingLeft="20dp" android:textColor="@android:color/white" android:textSize="20sp" android:textStyle="bold">
</TextView>        

2 个答案:

答案 0 :(得分:27)

您可以在另一个中包含一个布局文件,这种技术称为使用include标记重用Android布局。 e.g:

<include android:id="@+id/myid1" layout="@layout/workspace_screen" />

a blogpost by Android developers.中解释了这一点。

关于Android developer site explains the layout re-usability的另一篇文章。

答案 1 :(得分:4)

创建文件titlebar.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width=”match_parent”
    android:layout_height="wrap_content"
    android:background="@color/titlebar_bg">

    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content" 
               android:src="@drawable/gafricalogo" />
</FrameLayout>

在主要布局中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:background="@color/app_bg"
    android:gravity="center_horizontal">

    <include layout="@layout/titlebar"/>

    <TextView android:layout_width=”match_parent”
              android:layout_height="wrap_content"
              android:text="@string/hello"
              android:padding="10dp" />

    ...

</LinearLayout>