在 CoordinatorLayout 中的工具栏下方添加视图

问题描述 投票:0回答:5
android android-toolbar android-coordinatorlayout androiddesignsupport
5个回答
386
投票

获取属性

app:layout_behavior="@string/appbar_scrolling_view_behavior"

RecyclerView
上取下并将其放在您要在
FrameLayout
下显示的
Toolbar
上。

我发现滚动视图行为所做的一件重要的事情是将组件布局在工具栏下方。因为

FrameLayout
有一个会滚动的后代 (
RecyclerView
),所以
CoordinatorLayout
将获取用于移动
Toolbar
的滚动事件。


另一件事要注意:布局行为将导致

FrameLayout
高度调整大小 就好像
Toolbar
已经滚动
,并且随着
Toolbar
完全显示,整个视图被简单地向下推,因此视图的底部低于
CoordinatorLayout
的底部。

这对我来说是一个惊喜。我期望随着工具栏上下滚动,视图会动态调整大小。因此,如果您的视图底部有一个带有固定组件的滚动组件,则在完全滚动

Toolbar
之前,您将看不到该底部组件。

因此,当我想在 UI 底部锚定一个按钮时,我通过将按钮放在

CoordinatorLayout
(
android:layout_gravity="bottom"
) 的底部并在视图中添加等于按钮高度的底部边距来解决此问题工具栏下方。


95
投票

我设法通过添加来解决这个问题:

android:layout_marginTop="?android:attr/actionBarSize"

像这样到FrameLayout:

 <FrameLayout
        android:id="@+id/content"
        android:layout_marginTop="?android:attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       />

3
投票

使用折叠顶部工具栏或使用您自己选择的 ScrollFlags 我们可以这样做:从Material Design摆脱FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.coordinatorlayout.widget.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">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleGravity="top"
            app:layout_scrollFlags="scroll|enterAlways">


        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin">

            <ImageView
                android:id="@+id/ic_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_arrow_back" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="back"
                android:textSize="16sp"
                android:textStyle="bold" />

        </androidx.appcompat.widget.Toolbar>


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

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/post_details_recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="5dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            />

</androidx.coordinatorlayout.widget.CoordinatorLayout>


2
投票

从 Android studio 3.4 开始,您需要将此行放入包含

RecyclerView
的布局中。

app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"

0
投票

看一下。尝试给予与工具栏相同大小的边距

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior"
    tools:context=".MainActivity">

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

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" />

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


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            app:layout_anchor="@+id/constraintLayout"
            app:layout_anchorGravity="center"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.coordinatorlayout.widget.CoordinatorLayout>
© www.soinside.com 2019 - 2024. All rights reserved.