片段,CollapsingToolbar显示活动的工具栏工具栏、片段、CollapsingToolbar

2023-09-12 03:23:23 作者:爷丶天生傲骨

我有一个CollapsingToolbarLayout在我的片段,并呈现出精致,事情是,应用程序的工具栏也被显示。我尝试这个解决方案:Can我从抽屉式导航一个片段使用CollapsingToolbarLayout ,但不是为我工作...

I have a CollapsingToolbarLayout in my fragment and is showing fine, the thing is that App Toolbar is also being showed. I try this solution: Can I use CollapsingToolbarLayout in a Fragment from Navigation Drawer but is not working for me...

这是我的片段AppBarLayout:

This is my fragment AppBarLayout:

<android.support.design.widget.CoordinatorLayout
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.support.design.widget.AppBarLayout
    android:id="@+id/layout_bar"
    android:layout_width="match_parent"
    android:layout_height="192dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
        app:contentScrim="#FFf3802b"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/brandLogo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:adjustViewBounds="true"
            android:contentDescription="@string/logo_icon"
            android:scaleType="centerCrop"
            android:src="@drawable/avatar_krono"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/fragment_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
            app:layout_collapseMode="pin"/>

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

</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    ...

    </android.support.v4.widget.NestedScrollView>

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

我的父活动(activity_main.xml):

My parent activity (activity_main.xml):

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/appToolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

<!-- NavigationView to display slider menu -->
<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="280dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/drawer_view" />

</android.support.v4.widget.DrawerLayout>

我使用的myFragmentActivity(只是尝试),这条线来改变我APPToolBar的称号,它真的改变:

I'm using in myFragmentActivity (just to try) this line to change the title of my APPToolBar and it really change:

((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Title");

于是,我尝试用这条线来隐藏我的appToolbar但不工作,我appToolbar总是被显示:

Then, i try to hide my appToolbar by using this line but is not working, my appToolbar is always being showed:

((AppCompatActivity) getActivity()).getSupportActionBar().hide();

有什么建议?

推荐答案

您片段取代内层,而不是Acitvity工具栏。这一切都工作正常应该用碎片的容器和工具栏放在一起。在您的code:

Your fragment replaces the inner layer, but not Acitvity toolbar. That all worked properly should be placed together with fragment container and toolbar. In your code:

<!-- Use this LinearLayout for container -->
<LinearLayout
    android:id="@+id/correct_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/appToolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

当然,我们可以用另一种解决方案。我们可以在MainActivity创建功能,并呼吁在所有片段的实际关闭工具栏(执行其他code与你的不同)。等....

Sure we can use another solution. We can create function in MainActivity and call in all fragments for closing actual Toolbar (Perform other code different from yours). And other....