带有 post refs 的浮动按钮不浮动在右下角

Floating button not floating in bottom right with post refs

30 小时进入 android,现在进入更酷的东西。我试着按照这个 post

我的代码是下面的布局

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".base.ActivityContactList">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/contact_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/underline"
        android:scrollbars="vertical"
        />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16px"
        android:src="@drawable/plus_icon3"
        app:fabSize="normal"
        app:layout_anchor="@id/contact_list"
        app:layout_anchorGravity="bottom|right|end" />

</androidx.constraintlayout.widget.ConstraintLayout>

我有两个问题

这是一张图片:

我创建了图标视图“New -> Image Asset”,然后选择了自定义主题的剪贴画,这样我就可以设置颜色了。

  1. 如果您正在使用 ConstraintLayout 您可以这样做:
<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:src="@drawable/plus_icon3"
    app:fabSize="normal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />

现在图标将位于右下角。您应该会在 XML 布局文件中看到一条警告:This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints。在 ConstraintLayout 中,您总是必须至少设置一个水平和垂直约束。否则会跳转到0坐标。


  1. 绿松石色是您在 colors.xml 文件中的 colorAccent。要更改它,请添加:
app:backgroundTint="@color/yourColor"

Floating Action Button

My icon is not the bottom right

由于您正在使用 ConstraintLayout,因此您需要设置所需的约束条件。你的情况

layout_constraintBottom_toBottomOf="parent"
layout_constraintEnd_toEndOf="parent"

因此您的浮动操作按钮将变为

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16px"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:src="@drawable/ic_baseline_home_24"
    app:fabSize="normal" />

My icon has some turqoise color surrounding the purple plus and the purple plus is tiny. I am not sure why.

documentation 中所述,默认情况下它采用 styles.xml 属性 colorAccent 中设置的颜色。

  • 此视图的背景颜色默认为您主题的 colorAccent。如果你想在运行时改变它,那么你可以通过 setBackgroundTintList(ColorStateList).

所以你可以通过以下两种方法改变背景

  1. 在 XML、app:backgroundTint

    <com.google.android.material.floatingactionbutton.FloatingActionButton
         android:id="@+id/fab"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:backgroundTint="@color/colorPrimary"
         android:layout_margin="16px"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         android:src="@drawable/ic_baseline_home_24"
         app:fabSize="normal" />
    
  2. 以编程方式,.setBackgroundTintList

     mFab.setBackgroundTintList(ColorStateList.valueOf(your color in int));
    

使用类似于:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/..." />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

关于颜色请务必:

  • 使用最新的稳定版implementation 'com.google.android.material:material:1.1.0'
  • 使用 Theme.MaterialComponents.* 作为应用主题