繁体   English   中英

在CollapsingToolbarLayout中,工具栏在折叠状态下不可见

[英]Toolbar not visible in collapsed state inside CollapsingToolbarLayout

我在CollapsingToolbarLayout中有一个ViewPager。 当我向上滚动viewpager时,它可以正常滚动,但是当它折叠时,工具栏应该是可见的,但是它似乎停留在viewpager的后面,因为我向上滚动时可以看到工具栏的对齐。 我已经对stackoverflow进行了多次讨论,但没有找到解决方案。 请帮助我确定我在这里缺少什么。

[ 展开式 ] [10] [ 倒塌 ] [10]

这是我的布局xml-

<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:background="@color/colorAppBg"
           android:fitsSystemWindows="true">
    <android.support.design.widget.AppBarLayout
              android:id="@+id/appBarLayout"
              android:layout_width="match_parent"
              android:layout_height="300dp"
              android:fitsSystemWindows="true"
              android:theme="@style/AppTheme">
        <android.support.design.widget.CollapsingToolbarLayout
                 android:id="@+id/toolBarLayout"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:fitsSystemWindows="true"
                 app:contentScrim="?attr/colorPrimary"
                 app:layout_scrollFlags="scroll|snap|enterAlways"
                 app:scrimAnimationDuration="200"
                 app:scrimVisibleHeightTrigger="@dimen/scrimTrigger">
            <FrameLayout
                    android:id="@+id/frame"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:elevation="@dimen/actionBarElevation"
                    android:fitsSystemWindows="true"
                    app:layout_collapseMode="parallax">
                <android.support.v4.view.ViewPager
                       android:id="@+id/locationPager"
                       android:layout_width="match_parent"
                       android:layout_height="match_parent"
                       app:layout_collapseMode="parallax"/>
                <com.viewpagerindicator.CirclePageIndicator
                       android:id="@+id/locationPagerIndicator"
                       android:layout_width="match_parent"
                       android:layout_height="wrap_content"
                       android:layout_gravity="bottom"
                       android:layout_marginBottom="8dp"
                       app:fillColor="@color/colorText"
                       app:layout_collapseMode="parallax"
                       app:pageColor="@color/colorText"
                       app:radius="3dp"
                       app:strokeWidth="0dp"/>
            </FrameLayout>
            <android.support.v7.widget.Toolbar
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin">
                <TextView
                       android:id="@+id/titleView"
                       style="@style/TextViewNormal"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:text="@string/app_name"
                       android:textColor="@color/colorPrimary"/>
            </android.support.v7.widget.Toolbar>
        </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">
        <TextView
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:lineSpacingExtra="8dp"
                 android:padding="10dp"
                 android:text="hh"
                 android:textColor="@android:color/white"
                 android:textSize="20sp"/>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

在您的代码中尝试一下。在我的代码中效果很好。

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

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:elevation="@dimen/actionBarElevation"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax">
            <!-- add your code here -->
        </FrameLayout>


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

            <TextView
                android:id="@+id/titleView"
                style="@style/TextViewNormal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:textColor="@color/colorPrimary"/>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

我发现了问题。 问题是

android:elevation="@dimen/actionBarElevation"

在FrameLayout中

<FrameLayout
     android:id="@+id/frame"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:elevation="@dimen/actionBarElevation"
     android:fitsSystemWindows="true"
     app:layout_collapseMode="parallax">

删除此行代码后,它可以按预期工作。 但是我不知道这是什么问题。 但是我在编辑器中收到警告,说属性提升仅在API级别21和更高版本中使用。 我认为在api级为25的设备中运行应用程序时,它不会造成任何问题。应用程序的目标sdk是26,最小sdk是16。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM