首页 > 解决方案 > 如何在 kotlin 中实现折叠工具栏

问题描述

我有一个带有折叠工具栏的 XML 代码,当我单击尝试实现嵌套滚动条时,整个 xml 的对齐方式会发生变化。我尝试了很多方法。

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.RestaurantMenuActivity">


    <androidx.coordinatorlayout.widget.CoordinatorLayout
        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="50dp"
            android:elevation="0dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolMenuBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/red"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark" />
        </com.google.android.material.appbar.AppBarLayout>
        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="50dp"
            app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
            >
            <ImageView
                android:id="@+id/rest_image"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:src="@drawable/restaurant_image"
                />
        </com.google.android.material.appbar.CollapsingToolbarLayout>



        <RelativeLayout
            android:layout_marginTop="250dp"
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:background="@color/white"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">


            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerViewRestaurantMenu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp" />

            <RelativeLayout
                android:id="@+id/relativeLayoutProceedToCart"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:visibility="invisible">

                <Button
                    android:id="@+id/btnProceedToCart"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:background="@color/white"
                    android:text="proceed_to_cart_btn"
                    android:textColor="@color/white" />

            </RelativeLayout>
        </RelativeLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <RelativeLayout
        android:id="@+id/progressMenu"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">

        <ProgressBar
            android:theme="@style/CircularProgress"
            android:id="@+id/progressBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />


    </RelativeLayout>
</androidx.drawerlayout.widget.DrawerLayout>

我通过安排它尝试了很多次,但是一旦我为相对布局(id:frameLayout)引入嵌套滚动视图,整个视图就会崩溃。

标签: androidxmlkotlinandroid-collapsingtoolbarlayout

解决方案


推荐阅读