Android - 如何在AppBar中修复工具栏

时间:2017-07-27 13:38:10

标签: java android android-layout

我在滚动浏览内容时尝试修复工具栏。滚动时,当前工具栏会消失。我试过这个solution,但我没有设法让它发挥作用。到目前为止,我还没有解决方法。任何帮助将不胜感激。这是我的XML布局和相关布局的图片。

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

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="220dp"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

            <android.support.v4.view.ViewPager
                android:id="@+id/pager_most_read"
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="220dp"
                android:layout_alignParentTop="true"
                android:background="@android:color/transparent">
            </android.support.v4.view.ViewPager>

            <android.support.v7.widget.Toolbar
                android:layout_alignParentTop="true"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="#0000ff"
                app:layout_collapseMode="pin">
            </android.support.v7.widget.Toolbar>

            <me.relex.circleindicator.CircleIndicator
                android:layout_alignParentBottom="true"
                android:id="@+id/indicator"
                android:layout_width="match_parent"
                android:layout_height="48dp" />

        </RelativeLayout>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            app:tabIndicatorColor="@color/text_selected"
            app:tabMode="scrollable"
            app:tabSelectedTextColor="@color/text_selected"
            app:tabTextColor="@color/gray"/>

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


    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        />

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

enter image description here

1 个答案:

答案 0 :(得分:1)

已经有一段时间了,但我设法解决了,所以这里是代码:

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways"
            app:title="Title" />

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title">

            <!-- Action bar content -->

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <!-- scrollable content -->

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


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ... />

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