为什么我的片段会填满屏幕高度? - Android

时间:2014-10-27 12:57:50

标签: android android-layout android-fragments

我有这样的布局:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/wrapper"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background">

    <fragment
        android:id="@+id/fragment_header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="<headerClass>"/>

    <fragment
        android:id="@+id/fragment_body"
        android:layout_below="@id/fragment_header"
        android:layout_above="@+id/fragment_banner"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="<bodyClass>"/>

    <fragment
        android:id="@id/fragment_banner"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        class="<BannerClass>"/>

</RelativeLayout>

请注意,第一个片段的"wrap_content"为layout_height的参数。

此片段被定义为以下布局:

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

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/headerWrapper"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:background="@drawable/header_background">

        // stuff here...

    </RelativeLayout>

如您所见,高度设置为40dp

然而,标题占据了屏幕的整个高度。我不知道为什么。我正在看layoutInflater类,但由于我不熟悉它,我看不出任何问题。我用这一行来膨胀我的片段:

View view = inflater.inflate(R.layout.fragment_header, container, false);

为什么会显示此行为以及如何解决?

1 个答案:

答案 0 :(得分:0)

我设法通过将每个片段放在它自己的LinearLayout中并且给第一个高度为40dp来修复它。我不认为这是最好的解决方案,但这是我能想到的全部。