折叠工具栏布局不会折叠

问题描述 投票:0回答:4

我有一个折叠工具栏布局,它有两个问题:

1)它是静态的,不会崩溃,我找不到原因

2) recyclerview 中的最后一行在屏幕外显示了一半,我认为它应该被修复,崩溃是固定的?

XML代码:


<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Controllers.MainActivity">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
            app:contentScrim="@color/colorPrimary"
            app:title="Hero Title">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#000000" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hero Title"
                android:textStyle="bold"
                android:textSize="20dp"
                android:textColor="#ffffff"/>

            <android.support.v7.widget.Toolbar
                android:layout_width="match_parent"
                android:layout_height="150dp"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">

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

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/herosRecyclerView"
        android:layout_width="395dp"
        android:layout_height="715dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</android.support.design.widget.CoordinatorLayout>

这是我所说的半离屏的图像:

https://imgur.com/Fz8TmKK

android android-coordinatorlayout android-collapsingtoolbarlayout
4个回答
2
投票

请试试这个。它会起作用。

<?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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorWhite"
        tools:context=".Controllers.MainActivity">

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

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:textAlignment="center"
                app:contentScrim="@color/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:title="Hero Title">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="150dp"
                    android:background="#000000"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.7" />

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

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

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

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

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/herosRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>

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

0
投票

请将此添加到您的 ImageView:

app:layout_collapseMode="parallax"

0
投票

代码将如下所示:

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

    android:fitsSystemWindows="true"

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

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar"
        android:fitsSystemWindows="true"

        android:layout_height="192dp"
        android:layout_width="match_parent">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:toolbarId="@+id/toolbar"
            app:title="@string/app_name"


            app:layout_scrollFlags="snap|scroll|exitUntilCollapsed"
            app:layout_scrollInterpolator="@android:anim/decelerate_interpolator"

            app:contentScrim="?attr/colorPrimary">

            <ImageView
                android:id="@+id/app_bar_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

                android:src="@drawable/ss"
                android:scaleType="centerCrop" />

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_height="?attr/actionBarSize"
                app:collapseIcon="@drawable/ic_baseline_menu_24"
                app:navigationIcon="@drawable/ic_baseline_menu_24"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin"
                app:menu="@menu/demo_menu">

            </androidx.appcompat.widget.Toolbar>
        </com.google.android.material.appbar.CollapsingToolbarLayout>
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"


        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="12dp"
            android:text="@string/ipsum">

        </TextView>

    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

完整教程访问:

https://bedevelopers.tech/collapsing-toolbar-android-example-using-android-studio/


0
投票

就我而言,我的布局是这样的

<CoordinatorLayout>

 [App bar stuff]

 [RecyclerView]

</CoordinatorLayout>

我在回收站视图上删除了 marginBottom,它起作用了!

© www.soinside.com 2019 - 2024. All rights reserved.