包含另一个布局的布局填充视图

时间:2013-03-14 21:50:04

标签: android android-layout

我正在制作布局。 temp1包含在temp2中。 temp1工作正常,但是当我在temp2中包含它时。它填充整个屏幕,因为temp1中的根设置为fill_parent。在这种情况下可能有什么解决方案?

我想在temp1中显示中心temp2的一个小区域中的布局。

temp1.xml

<RelativeLayout android:layout_height="fill_parent"
                 android:layout_width="fill_parent">


</RelativeLayout>

temp2.​​xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:id="@+id/relativeLayout">
    <include
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/temp1"/>
</RelativeLayout>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

您可以覆盖所包含布局的根视图的layout_*字段,在这种情况下,它们会覆盖RelativeLayout中的根@layout/temp1

你可以使用保证金,并将其调整到你想要的边界量(100dp只是一个例子):

<include
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="100dp"
        layout="@layout/temp1"/>

或者你可以设置你想要的大小(100dp只是一个例子):

<include
        android:layout_width="100dp"
        android:layout_height="100dp"
        layout="@layout/temp1"/>

或者你可以只显示那里的内容(temp1目前没有任何内容):

<include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/temp1"/>

有关详细信息,请参阅http://developer.android.com/training/improving-layouts/reusing-layouts.html