【问题标题】:Android: unable to hide appbar on scrollingAndroid:无法在滚动时隐藏应用栏
【发布时间】:2019-12-23 07:15:19
【问题描述】:

我正在尝试在滚动时隐藏 appbar,但它不起作用。这是主要布局:

    <?xml version="1.0" encoding="utf-8"?>
    <layout 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"
        tools:context=".MainActivity">
        <data>
            <variable
                name="user"
                type="com.userdashboard.model.User_dash" />
        </data>
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/main_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

                <include
                    android:id="@+id/content"
                    layout="@layout/appbar_user_dash"
                    bind:user="@{user}"/>


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

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

                    </LinearLayout>

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

           </LinearLayout>

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

</Layout>

我在主布局中包含了 appbar:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

        <variable
            name="user"
            type="com.userdashboard.model.User_dash" />
    </data>

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="255dp"
        android:background="#181F29">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_margin="16dp"
                    android:contentDescription="@string/app_name"
                    android:src="@drawable/ic_share" />

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentStart="true"
                    android:layout_margin="16dp">

                    <ImageView
                        android:id="@+id/imv_setting"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="8dp"
                        android:contentDescription="@string/app_name"
                        android:src="@drawable/ic_settings" />

                    <ImageView
                        android:id="@+id/imv_message"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_toEndOf="@id/imv_setting"
                        android:contentDescription="@string/app_name"
                        android:src="@drawable/ic_message" />
                </RelativeLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:layout_marginTop="104dp"
                    android:background="@drawable/curve_appbar"
                    android:gravity="end"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:layout_marginEnd="140dp"
                        android:hint="userName"
                        android:text="@{user.firstName}"
                        android:textColor="@android:color/white"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_marginEnd="140dp"
                        android:gravity="end"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:hint="1398/06/21"
                            android:paddingEnd="8dp"
                            android:text="@{user.createDate}"
                            android:textColor="@android:color/white"
                            android:textSize="14sp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:paddingEnd="8dp"
                            android:text="date join:"
                            android:textColor="#616E81"
                            android:textSize="16sp" />


                        <ImageView
                            android:id="@+id/imageView"
                            android:layout_width="24dp"
                            android:layout_height="24dp"
                            android:contentDescription="@string/app_name"
                            android:scaleType="centerInside"
                            android:src="@drawable/ic_datetime" />


                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_marginEnd="140dp"
                        android:gravity="end"
                        android:orientation="horizontal">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:hint="test"
                            android:paddingEnd="8dp"
                            android:text="@{user.jobLocations.name}"
                            android:textColor="@android:color/white"
                            android:textSize="14sp" />

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:paddingEnd="8dp"
                            android:text="service:"
                            android:textColor="#616E81"
                            android:textSize="16sp" />

                        <ImageView
                            android:layout_width="24dp"
                            android:layout_height="24dp"
                            android:contentDescription="@string/app_name"
                            android:scaleType="centerInside"
                            android:src="@drawable/ic_service_location" />

                    </LinearLayout>

                </LinearLayout>

                <ImageView
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="80dp"
                    android:layout_marginEnd="50dp"
                    android:background="@drawable/image_border"
                    android:contentDescription="@string/app_name"
                    android:src="@{user.userImage}" />

                <RelativeLayout
                    android:layout_width="90dp"
                    android:layout_height="20dp"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="148dp"
                    android:layout_marginEnd="46dp"
                    android:background="#CD181F29">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:text="pic"
                        android:textColor="@android:color/white"
                        android:textDirection="rtl" />
                </RelativeLayout>

                <RelativeLayout
                    android:layout_width="80dp"
                    android:layout_height="25dp"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="175dp"
                    android:layout_marginEnd="46dp"
                    android:background="@drawable/round_edge_shape">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:text="@{user.user_JobLocations}"
                        android:textColor="@android:color/white"
                        android:textDirection="rtl"
                        android:textStyle="bold" />

                </RelativeLayout>

            </RelativeLayout>

        </android.support.v7.widget.Toolbar>

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

</layout>

正如您在我的 xml 代码中看到的那样,我正在使用 android 数据绑定,但我将 app:layout_scrollFlags="scroll|enterAlways" 添加到工具栏,并将 app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到主布局中的 NestedScrollView,但是当我运行应用程序时,工具栏没有隐藏在滚动。 这里有什么问题?

【问题讨论】:

  • &lt;AppBarLayout&gt; 必须是 &lt;CoordinatorLayout&gt; 的直接子代。如果第二个布局是包含的appbar_user_dash,那么它在&lt;LinearLayout&gt; 内。将&lt;include&gt; 移出&lt;LinearLayout&gt;
  • 你试过用这个:app:layout_scrollFlags="scroll|exitUntilCollapsed"而不是这个app:layout_scrollFlags="scroll|enterAlways"
  • 是的,但没有任何改变@GOVINDDIXIT
  • 实际上,如果&lt;LinearLayout&gt; 中只有&lt;include&gt;&lt;NestedScrollView&gt;,那么您根本不需要最外面的&lt;LinearLayout&gt;。只需将它们都直接放入&lt;CoordinatorLayout&gt;

标签: android android-scroll


【解决方案1】:

感谢@MikeM,我将主布局更改为:

<?xml version="1.0" encoding="utf-8"?>
<layout 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"
    tools:context=".MainActivity">
    <data>
        <variable
            name="user"
            type="com.userdashboard.model.User_dash" />
    </data>
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <include
            android:id="@+id/content"
            layout="@layout/appbar_user_dash"
            bind:user="@{user}" />

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

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

现在 appbar 滚动了 :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多