【问题标题】:Scroll only FrameLayout in AppBarLayout在 AppBarLayout 中仅滚动 FrameLayout
【发布时间】:2019-04-06 18:10:20
【问题描述】:

我的应用在 AppBarLayout 中有 3 种布局:Toolbar、FrameLayout 和 LinearLayout。当我向上滚动 NestedScrollView 时,必须隐藏 FrameLayout 但工具栏必须留在他的位置,并且 LinearLayout 必须在工具栏下。在当前情况下,没有滚动任何内容。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorFilters"
            />

        <FrameLayout
            android:id="@+id/userinfo_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|snap" />

        <LinearLayout
            android:id="@+id/navigation_tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
           />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.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="30dp"
            android:text="@string/fully_text"
            android:textSize="20sp" />

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

    标签: android material-design android-coordinatorlayout android-appbarlayout


    【解决方案1】:

    使用以下代码,它肯定会工作 根据您的需要进行成型

    <?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <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:focusable="true"
                android:focusableInTouchMode="true"
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize"
                android:background="@color/white"
                app:contentInsetStartWithNavigation="0.0dip"
                app:layout_scrollFlags="enterAlways|scroll"
                app:titleTextColor="@color/black"
                android:contentInsetEnd="0dp"
                android:contentInsetLeft="0dp"
                android:contentInsetRight="0dp"
                android:contentInsetStart="0dp"
                app:contentInsetEnd="0dp"
                app:contentInsetLeft="0dp"
                app:contentInsetRight="0dp"
                app:contentInsetStart="0dp" >
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_vertical"
                    android:orientation="horizontal">
    
                    <ImageButton
                        android:id="@+id/close_activity"
                        android:layout_width="?actionBarSize"
                        android:layout_height="?actionBarSize"
                        android:background="?selectableItemBackgroundBorderless"
                        android:tint="@color/my_green"
                        app:srcCompat="@drawable/ic_close_black_24dp"
                        tools:ignore="VectorDrawableCompat" />
    
                    <TextView
                        android:textSize="20dp"
                        android:textStyle="bold"
                        android:textColor="@color/black"
                        android:text="Profile"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />
    
                </LinearLayout>
    
            </android.support.v7.widget.Toolbar>
    
        </android.support.design.widget.AppBarLayout>
    
        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none"
            android:scrollingCache="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:lineSpacingExtra="8dp"
                android:padding="30dp"
                android:text="@string/fully_text"
                android:textSize="20sp" />
    
        </androidx.core.widget.NestedScrollView>
    
    </android.support.design.widget.CoordinatorLayout>
    

    希望对你有帮助

    编码愉快 :)

    任何问题

    让我知道

    【讨论】:

    • frameLayout应该在哪里?如果一个应用有 2 个布局,那不是问题,但我问了 3 个布局,情况会有所不同
    • 工具栏内只保留一个视图。假设您保留了一个线性布局,然后将 iu 想要保留的任何内容保留在该线性布局中
    • @Delphian 看看我的代码,我是如何在工具栏中使用线性布局在其中放置其他视图的
    • 您将 LinearLayout 放入 Toolbar 这意味着布局将具有与工具栏相同的动画。但我想要不同的布局动画。我需要固定工具栏,隐藏 FrameLayout(向上滚动时),并且 LinearLayout 也必须滚动,以便将其放置在工具栏下方。
    • 使用我所说的线性布局内的任何布局并应用您想要的任何动画
    猜你喜欢
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多