如何在通用协调器布局中修复Fragment中的FAB



如何在common CoordinatorLayout中修复Fragment中的FAB

我想知道在这种情况下处理视图(例如浮动操作按钮(的最佳或更好的做法。

问题

目前,我实现了一个通用的工具栏,它可以像隐藏或出现在"活动"中以在某些片段中显示。然后,我想在特定的Fragment上实现一个FAB。但当我在Fragment中实现FAB时,它移动了ToolBar的高度。

故障图像

我想

我想把它固定在屏幕的底部和右侧。

我该如何解决这个问题?如果你不介意,请告诉我这件事。

我想…

我的想法

我认为FAB应该放在活动中。如果进展顺利,我想根据片段改变可见性和不同的角色。有可能。。。?(作为Twitter(

我的英语很差,但我很感激你的好意。

代码(摘录(

活动

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout       
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
>
<androidx.appcompat.widget.Toolbar         
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"           
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

片段


<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
-- some constraints--
android:layout_margin="16dp"/>
</<androidx.constraintlayout.widget.ConstraintLayout>

使用android:layout_gravity="bottom|start">而不是layout_contraintBottom_toBottomOf来修复布局底部的FAB。只能在ConstraintLayout中使用约束。

如果你想使用CoordinatorLayout,你应该使用android:layout_gravity。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_gravity="bottom|start"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

并确保您在项目中使用androidx。如果没有,请更改

androidx.coordinatorlayout.widget.CoordinatorLayout

android.support.design.widget.CoordinatorLayout

在您的问题中,您正在片段xml中使用ConstraintLayout。将其更改为CoordinatorLayout

更新

在活动XML中使用ConstraintLayout。将XML更改为:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:fitsSystemWindows="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="@+id/fragmentContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/appBar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:layout="@layout/your_fragment_layout" />
</androidx.constraintlayout.widget.ConstraintLayout>

并且片段不需要更改。

最新更新