定位布局有多个包含

时间:2013-10-26 14:33:13

标签: android layout

为什么这些:

<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"
    tools:context=".MainActivity" >

    <include 
        layout="@layout/myCustomTitleBar"
        android:id="@+id/titlebar" />

    <include
        layout="@layout/myCustomToolBar"
        android:id="@+id/toolbar"
        android:layout_below="@id/titlebar" />

    <com.myname.myapp.MyCustomView
        android:id="@+id/myView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar"
    />

</RelativeLayout>

不工作?
两个include彼此堆叠/重叠。 (layout_below无法正常工作)
include s - myCustomTitleBarmyCustomToolBar - 都是RelativeLayout s。 我做错了吗?

期望的结果:

illustration

2 个答案:

答案 0 :(得分:3)

根据您的应用草图,您不应以root身份使用RelativeLayoutRelativeLayout完全用于覆盖项目。您必须使用LinearLayout。当然,LinearLayout组的每个项目都必须具有适当的layout_width/layout_height值。

<强>根:

<?xml version="1.0" encoding="utf-8"?>

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

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

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

    <RelativeLayout android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:background="#991022"/>

</LinearLayout>

<强> relative_sample_a.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#AACC33">

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="ButtonA"/>

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_alignParentRight="true"
            android:text="ButtonB"/>


</RelativeLayout>

<强> relative_sample_b.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#DDBB00">

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_centerInParent="true"
            android:text="ButtonC"/>


</RelativeLayout>

enter image description here

答案 1 :(得分:1)

尝试将android:layout_alignParentTop="true"添加到标题栏

<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"
    tools:context=".MainActivity" >

    <include 
        layout="@layout/myCustomTitleBar"
        android:id="@+id/titlebar"
        android:layout_alignParentTop="true" />

    <include
        layout="@layout/myCustomToolBar"
        android:id="@+id/toolbar"
        android:layout_below="@id/titlebar" />

    <com.myname.myapp.MyCustomView
        android:id="@+id/myView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/toolbar"
    />

</RelativeLayout>