本文介绍了当AppBar折叠/展开时,如何使工具栏固定在顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实施滚动技术内容重叠的灵活空间,如材料设计中所述.

I am trying to implement one of the Scrolling Techniques, Flexible space with overlapping content, described in Material Design.

内容可以与应用栏重叠.

Content can overlap the app bar.

行为:

应用栏的开始位置应位于内容后面. 向上滚动时,应用栏的滚动速度应比内容滚动的速度快, 直到内容不再重叠为止.固定到位后, 应用栏会抬起,以允许内容在下方滚动.

The app bar’s starting position should be located behind the content. Upon upward scroll, the app bar should scroll faster than the content, until the content no longer overlaps it. Once anchored in place, the app bar lifts up to allow content to scroll underneath.

https://www. google.co.in/design/spec/patterns/scrolling-techniques.html#scrolling-techniques-scrolling


问题

但是,问题是,展开后,我的AppBar中的标题会向下滚动,并隐藏在重叠的内容下方.


Problem

However, the problem is,the title in my AppBar scrolls down when expanded and hides below the overlapping content.

在这里,我的工具栏隐藏在重叠的CardView下面.

Here, my toolbar is hidden below the overlapping CardView.

折叠应用栏时,工具栏和标题"将从下方向上滑动.

When the appbar is collapsed, the toolbar and hence the Title slides up from below.

这是我的代码:

activity-main.xml

activity-main.xml

<android.support.design.widget.CoordinatorLayout
    ...
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            ...
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="top"
                android:background="?attr/colorPrimary"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
        </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"
        ...

我还在MainActivity的onCreate函数中添加了这些

I have also added these in my MainActivity's onCreate function

    setSupportActionBar(toolbar);

    collapsingToolbarLayout.setTitle("App Name");


我希望工具栏(带有磁贴和其他内容,稍后将添加)保持在顶部,而与应用栏的展开或折叠无关.


I want the toolbar(with the tile and the other contents, which I will add later) to stay at the top irrespective of the appbar being expanded or collapsed.

我已经阅读了文档,浏览了许多帖子和教程,观看了很多视频,但是根本找不到有效的解决方案或任何相关的解决方案.

I have read the documentations, gone through many posts and tutorials, watched a lot of videos but failed to find a working solution or any related solutions at all.

如果有人对如何解决此问题有任何想法,请提出建议.感谢您的帮助.

If anyone has some idea on how to fix this, please suggest. Thanks for helping.

推荐答案

当我在类似问题报告的评论中找到答案时,我正在寻找解决方案.

I was looking for a solution myself when I found the answer in the comments on a similar issue report.

基本上,您像这样在CollapsingToolbarLayout上调用setTitleEnabled():

Basically you call setTitleEnabled() on your CollapsingToolbarLayout like this:

CollapsingToolbarLayout.setTitleEnabled(false);

您也可以通过将其添加到CollapsingToolbarLayout中来在xml中执行此操作:

You can do this in xml as well, by adding this to your CollapsingToolbarLayout:

app:titleEnabled="false"

通过将其设置为false,您将获得所需的行为.标题保持固定在屏幕顶部.

By setting it to false, you'll get the desired behaviour. The title stays fixed to the top of the screen.

Toolbar本身实际上已经在顶部,但这使标题也保持在顶部,而不是在CollapsingToolbarLayoutToolbar的底部之间进行翻译.

The Toolbar itself was actually already at the top, but this makes the title stay there as well, instead of translating between the bottom of the CollapsingToolbarLayout and the Toolbar.

这篇关于当AppBar折叠/展开时,如何使工具栏固定在顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:55