在工具栏下方添加片段

Adding fragment below toolbar

我有一个activity.xml

如您所见,这是一个相对布局。我希望包含片段的框架布局将位于工具栏下方。

确实添加了片段。但它被添加在顶部,覆盖了工具栏。只是想知道我做错了什么。

该网站出于某种原因不允许我添加代码。它告诉我它的格式不正确。这是 activity.xml 布局。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    >

    <!--scroll|snap-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/appToolBar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolBarHeight"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <FrameLayout
        android:id="@android:id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/appToolBar"/>

</RelativeLayout>

这就是我在 Activity.java 文件中所做的。

getFragmentManager().beginTransaction().add(android.R.id.content, SetupFrag.createInstance(false), SETUP_FRAGMENT).commit();

您应该使用不同的根布局。

RelativeLayout 将相对于彼此布置其子项。 您想要的是垂直方向的 LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    >

    <android.support.v7.widget.Toolbar
        android:id="@+id/appToolBar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolBarHeight"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

您使用的是现有 ID,@android:id/content

您应该使用 + 添加您自己的 ID,@+id/content,否则片段将被添加到内容视图(您的 activity 布局之上的级别)。