如何在向上滚动内容时隐藏工具栏?

39

我想在滚动文本和图片时隐藏我的工具栏。我使用scrollView获取滚动内容。当我向上滚动内容时,如何隐藏工具栏?

这是我的XML代码:

content_main.XML:

<android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
app:layout_behavior="@string/appbar_scrolling_view_behavior">


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

<LinearLayout
    android:paddingTop="?android:attr/actionBarSize"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:id="@+id/textone"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="23dp"
        android:textStyle="bold"
        android:text="hello world jheds sdjhs jds sjbs skjs ksjs kksjs ksj sdd dskd js sk "/>

    <ImageView
        android:id="@+id/imge"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:src="@drawable/imag_bg"/>


    <TextView
        android:id="@+id/texttwo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:text="Pretty good, the Toolbar is moving along with the list and getting back just as we expect it to. This is thanks to the restrictions that we put on the mToolbarOffset variable.
        If we would omit checking if it’s bigger than 0 and lower than mToolbarHeight then when
        we would scroll
         up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing.



          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing

          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing

          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing

          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing

          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing
        It works pretty well, but this is not what we want. It feels weird that you can
        stop it in the middle of
        the

         scroll and the Toolbar will stay half visible. Actually this is how it’s done in Google Play
         Games app
        which I consider as a bug

        It works pretty well, but this is not what we want. It feels weird that you can
        stop it in the middle of
        the
         scroll and the Toolbar will stay half visible. Actually this is how it’s done in Google Play
         Games app
        which I consider as a bug
        It works pretty well, but this is not what we want. It feels weird that you can
        stop it in the middle of
        the
         scroll and the Toolbar will stay half visible. Actually this is how it’s done in Google Play
         Games app
        which I consider as a bug."/>




</LinearLayout>

<View
    android:layout_width="wrap_content"
    android:layout_height="30dp" />
<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
         android:text="hai"
         android:layout_width="160dp"
         android:layout_height="match_parent" />
    <Button
         android:text="hello"
         android:layout_width="160dp"
         android:layout_height="match_parent" />


</LinearLayout>

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

activity_main.XML

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent" 
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar"
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" 
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

<include layout="@layout/content_main" />

here my image


请尝试访问以下链接:https://mzgreen.github.io/2015/06/23/How-to-hideshow-Toolbar-when-list-is-scrolling%28part3%29/ - K Neeraj Lal
我这里没有使用ListView和浮动操作按钮 @KNeerajLal - chanti
5个回答

62

你需要对你的布局做很多的改变。首先在activity_main.XML中使用CoordinatorLayout,如下所示(根据你的需求改变主题)。

<?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"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

    <include layout="@layout/content_main" />

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

content_main.XML中使用android.support.v4.widget.NestedScrollView替代ScrollView

还需在android.support.v4.widget.NestedScrollView内使用app:layout_behavior="@string/appbar_scrolling_view_behavior",如下所示。

<android.support.v4.widget.NestedScrollView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <TextView
                android:id="@+id/textone"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="hello world jheds sdjhs jds sjbs skjs ksjs kksjs ksj sdd dskd js sk "
                android:textSize="25dp"
                android:textStyle="bold" />

            /// Add your other code here

            </LinearLayout>

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

2
当我滚动内容时,我的工具栏名称会在状态栏中显示。 - chanti
我发布了我的输出图像,我在我的XML中使用了你的代码,但我的输出结果与上面的不同@Dhawal Sodha Parmar - chanti
4
从activity_main.XML文件的CoordinatorLayout中删除android:fitsSystemWindows="true"。 - Dhaval Parmar
现在它已经可以工作了,但是我遇到了另一个问题,我的状态栏颜色显示为白色@Dhawal Sodha Parmar - chanti
在v21的style.xml中使用<item name="android:statusBarColor">@color/colorPrimary</item>。 - Dhaval Parmar

2
这是将CoordinatorLayout应用到你的app的最佳方案。 CoordinatorLayout是一个强大的FrameLayout,有很多巧妙的动画技巧。

Design库引入了CoordinatorLayout,这是一个布局,它在子视图之间提供了额外的触摸事件控制级别,许多设计库中的组件都充分利用了它。

你可以从这个这个教程开始。

我在工具栏中添加了这行代码,但仍然没有得到输出。 app:layout_scrollFlags="scroll|enterAlways" @Aritra Roy - chanti

0

0
将 activity_main.xml 包装在 Coordinator Layout 中,使其成为其父布局。
<android.support.design.widget.CoordinatorLayout  
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:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent" 
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar"
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary" 
    app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />



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

0

只需将标志设置为 app:layout_scrollFlags="scroll|enterAlways",就可以实现滚动效果。

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:titleTextColor="#FFFFFF"
    app:layout_scrollFlags="scroll|enterAlways"   
    />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接