滚动时为什么工具栏没有隐藏?

时间:2016-01-21 11:33:22

标签: android android-toolbar

我正在使用工具栏和多个带有一些文本的Textview,但是当我滚动我试图隐藏我的工具栏但它没有隐藏时,以下是我的xml代码可以任何人帮助我这个,谢谢提前.. < / p>

<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:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:fitsSystemWindows="true">



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



    </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">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:orientation="vertical"

            android:paddingTop="24dp">



            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Product Name:"
                android:layout_marginLeft="5dp"
                android:textAppearance="@style/TextAppearance.AppCompat.Title" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/desc_prodname"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"

                android:text="@string/cheese_ipsum"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Brand:"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="5dp"

                android:textAppearance="@style/TextAppearance.AppCompat.Title" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/desc_brandnm"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"
                android:text="@string/cheese_ipsum"

                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Description:"
                android:layout_marginLeft="5dp"

                android:textAppearance="@style/TextAppearance.AppCompat.Title" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/desc_description"
                android:layout_marginLeft="5dp"
                android:text="@string/cheese_ipsum"

                android:layout_marginTop="5dp"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Price:"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="5dp"

                android:textAppearance="@style/TextAppearance.AppCompat.Title" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/desc_price"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"
                android:text="@string/cheese_ipsum"

                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Category:"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="5dp"

                android:textAppearance="@style/TextAppearance.AppCompat.Title" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/desc_catname"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"

                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Subcategory:"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="5dp"

                android:textAppearance="@style/TextAppearance.AppCompat.Title" />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/desc_subcatname"
                android:layout_marginTop="5dp"
                android:layout_marginLeft="5dp"

                />






        </LinearLayout>




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

    <!--<android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        app:layout_anchorGravity="bottom|right|end"
        android:src="@drawable/ic_discuss"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"/>-->

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

4 个答案:

答案 0 :(得分:2)

将工具栏放在&#34; NestedScrollView&#34;

答案 1 :(得分:1)

也许这会有所帮助? https://github.com/ksoichiro/Android-ObservableScrollView

您可以在必要时观察滚动区域并显示/隐藏工具栏。

答案 2 :(得分:1)

将此代码用于工具栏:

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

解释: 必须启用属性app:layout_scrollFlags中使用的滚动标志才能使任何滚动效果生效。

有关更多信息和不同的滚动效果,请检查Handling-Scrolls-with-CoordinatorLayout

答案 3 :(得分:0)

如果您想在不使用任何库的情况下实现这一目标,则必须将工具栏放在CollapsingToolbarLayout中。这是工作布局。

<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.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="256dp"
    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"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">


        <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" />

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

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

// put your contents here..

</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
相关问题