【问题标题】:FloatingActionButton, layout_anchor and layout_gravityFloatingActionButton、layout_anchor 和 layout_gravity
【发布时间】:2016-09-23 17:35:20
【问题描述】:

这里有点新手。我已经进行了大约两个月的 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_anchorlayout_gravity。没有锚标记,它看起来像这样:

所以这是我的问题:为什么我的锚会搞砸我的FloatingActionButton 的定位?我做错了什么?

【问题讨论】:

    标签: android android-layout floating-action-button layout-gravity layout-anchor


    【解决方案1】:

    您只需添加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" />
    

    【讨论】:

      【解决方案2】:

      如果您使用约束布局,您应该像这样将 fab 的顶部和底部约束设置为您想要的布局边缘。例如,我有一个 cardview 女巫,我想将工厂锚定到它

      <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"
          />
      

      【讨论】:

        猜你喜欢
        • 2015-10-14
        • 1970-01-01
        • 2016-02-06
        • 1970-01-01
        • 2015-02-15
        • 2015-11-30
        • 1970-01-01
        • 2020-12-07
        • 2015-08-18
        相关资源
        最近更新 更多