一个片段中状态栏后面的工具栏而不是另一个片段

DJ-*_*DOO 6 android android-fragments android-toolbar android-coordinatorlayout

所以我有一个活动和两个片段。

我正在添加片段 1,工具栏按预期显示在此处输入图片说明

但是,fragment2 中的工具栏出现在工具栏后面。在此处输入图片说明

编辑

所以有两种显示Fragment 2的方式,第一种是通过在Fragment 1中点击显示,另一种是在启动主机活动时显示Fragment 2。无论如何,每次都调用相同的方法。但是,当直接从 Activity 启动 Fragment2 时,工具栏会按预期显示,但如果从 Fragment1 启动,则 Fragment 2 工具栏显示如下。

所以我认为问题是我不允许将工具栏切换为 supportActionBar?在片段 1 中,我将片段工具栏设置为操作栏,片段 2 工具栏也是如此。有没有办法做到这一点?

所以我的设置方式是我的活动布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/location_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<FrameLayout
    android:id="@+id/area_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

我正在使用两个框架布局,因为我希望在从片段 2 返回时仍显示片段 1。

fragment1 的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:daft="http://schemas.android.com/tools"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/search_results_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/test_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:theme="@style/HomeScreenToolbarTheme"
            my:titleTextAppearance="@style/DaftTheme.ActionBarStyle.TitleTextStyle"
            app:layout_scrollFlags="scroll|enterAlways">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:id="@android:id/empty"
            android:visibility="gone"
            style="@style/daft_progressbar_text"
            android:padding="@dimen/activity_horizontal_margin"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/no_results" >
        </TextView>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/results_a_recyclerview"
            android:clipToPadding="false"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v7.widget.RecyclerView>
    </RelativeLayout>

    <ViewStub android:id="@+id/results_a__snackbar"
        android:inflatedId="@+id/results_a__snackbar_layout"
        android:layout="@layout/my_snack_bar"
        android:layout_gravity="bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

fragment2 的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:daft="http://schemas.android.com/tools"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/results_b_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/areas_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:theme="@style/HomeScreenToolbarTheme"
            my:titleTextAppearance="@style/DaftTheme.ActionBarStyle.TitleTextStyle"
            app:layout_scrollFlags="scroll|enterAlways">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>
    <RelativeLayout
        android:layout_marginBottom="@dimen/button_height"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:id="@android:id/empty"
            android:visibility="gone"
            style="@style/daft_progressbar_text"
            android:padding="@dimen/activity_horizontal_margin"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/no_results"
            android:background="@color/white">
        </TextView>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/results_b_recyclerview"
            android:background="@color/white"
            android:clipToPadding="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>
    </RelativeLayout>

    <Button
        android:id="@+id/clear_button"
        style="@style/reset_button_standard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/create_search_reset_button"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/confirm_button"
        />

    <Button
        android:id="@+id/confirm_button"
        style="@style/confirm_button_standard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/confirm"
        android:layout_gravity="bottom"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

最后是我用于工具栏和活动的样式

   <style name="HomeScreenTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:colorAccent">@color/primary_blue</item>
        <item name="colorAccent">@color/primary_blue</item>
        <item name="colorPrimary">@color/primary_blue</item>
        <item name="colorPrimaryDark">@color/primary_blue_dark</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

    <style name="HomeScreenToolbarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:fitsSystemWindows">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

两个工具栏都设置为onCreateView片段中的操作栏,例如

toolbar1 = view.findViewById(R.id.test_toolbar);

        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.setSupportActionBar(cityCountyToolbar);

        ActionBar actionBar = activity.getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("Fragment 1");
Run Code Online (Sandbox Code Playgroud)

我对这里发生的事情感到困惑,两个片段都应用相同的工具栏样式,两个片段都是具有该样式的一个活动的一部分。除了片段 2 中的一些添加之外,片段布局几乎相同。

顺便说一句,工具栏与回收器视图一起滚动,这是预期的行为。

<item name="android:windowTranslucentStatus">true</item>已在样式中设置。我不确定因为两个片段工具栏都使用相同的样式