有点新手的感觉。我有两个月的Android开发经验,但我在其他环境中有多年的开发经验。
好吧。我有一个FloatingActionButton,它没有出现在我期望或想要的地方。它在CoordinatorLayout中,与AppBarLayout/Toolbar一起,跟在ListView之后。
布局如下:
<?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:id="@+id/fragment_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ViewVehicleList">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:title="Vehicle List"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>
<ListView
android:id="@+id/Vehicle_ListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#FFFFFF"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</ListView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_AddVehicle"
style="@style/FloatingAddButton"
android:src="@drawable/ic_green_add"
android:layout_gravity="bottom|end"
app:layout_anchor="@id/Vehicle_ListView"
android:onClick="addVehicle"/>
</android.support.design.widget.CoordinatorLayout>使用这种布局,屏幕看起来如下所示:

我的layout_gravity上写着"bottom|end"。我将其更改为"bottom|right",但仍然得到相同的结果。我已经阅读了很多教程,并通过Stack Overflow进行了研究,但一直没有成功。
我设法解决了这个问题,删除了FAB元素app:layout_anchor="@id/Vehicle_ListView"中列出的锚点,这似乎与我所读到的内容相反:要使用FAB并正确定位它,您需要使用layout_anchor和layout_gravity。没有锚标签,它看起来是这样的:

所以我的问题是:为什么我的主播搞砸了我的FloatingActionButton的定位?我做错了什么?
发布于 2016-08-18 00:28:20
您只需添加layout_anchorGravity即可。
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_AddVehicle"
style="@style/FloatingAddButton"
android:src="@drawable/ic_green_add"
android:onClick="addVehicle"
app:layout_anchor="@id/Vehicle_ListView"
app:layout_anchorGravity="bottom|end" />发布于 2019-09-16 02:07:30
如果您使用约束布局,则应将fab的顶部和底部约束设置为所需的布局边,如下所示。例如,我有一个cardview,我想将fab锚定到它。
<androidx.cardview.widget.CardView
android:id="@+id/mix_video_cardView/>
<com.google.android.material.floatingactionbutton.FloatingActionButto
app:layout_constraintTop_toBottomOf="@+id/mix_video_cardView"
app:layout_constraintBottom_toBottomOf="@+id/mix_video_cardView"
app:layout_constraintStart_toStartOf="@+id/mix_video_cardView"
app:layout_constraintEnd_toEndOf="@+id/mix_video_cardView"
/>https://stackoverflow.com/questions/37448571
复制相似问题