FloatingActionButton与RecyclerView

huangapple go评论61阅读模式
英文:

FloatingActionButton with RecyclerView

问题

我的FloatingActionButton呈灰色。我尝试使用CoordinatorLayout,但它不起作用。以下是问题的屏幕截图和代码片段。

FloatingActionButton与RecyclerView

<?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"
    style="@style/AppTheme">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/alarms_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:scrollbars="vertical"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="83dp"
        android:layout_height="61dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:src="@drawable/ic_add_alarm_black_24dp"
        app:backgroundTint="#FFFFFF"
        app:fabSize="normal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"></com.google.android.material.floatingactionbutton.FloatingActionButton>
</androidx.constraintlayout.widget.ConstraintLayout>
英文:

My FloatingActionButton is greyed out. I've tried using the Coordinator layout but it isn't working. Below are the screenshot of the problem and a code snapshot.

FloatingActionButton与RecyclerView

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    style=&quot;@style/AppTheme&quot;&gt;
    &lt;androidx.recyclerview.widget.RecyclerView
        android:id=&quot;@+id/alarms_list&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginStart=&quot;16dp&quot;
        android:layout_marginTop=&quot;16dp&quot;
        android:scrollbars=&quot;vertical&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
    &lt;com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id=&quot;@+id/fab&quot;
        android:layout_width=&quot;83dp&quot;
        android:layout_height=&quot;61dp&quot;
        android:layout_marginEnd=&quot;16dp&quot;
        android:layout_marginBottom=&quot;16dp&quot;
        android:src=&quot;@drawable/ic_add_alarm_black_24dp&quot;
        app:backgroundTint=&quot;#FFFFFF&quot;
        app:fabSize=&quot;normal&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;&gt;&lt;/com.google.android.material.floatingactionbutton.FloatingActionButton&gt;
&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案1

得分: 1

首先,如果您使用 AndroidX,请确保将以下依赖项添加到您的应用 gradle 文件中:

implementation 'com.google.android.material:material:1.0.0'

接下来,将您的 XML 中的 FAB 替换为以下代码。FAB 应出现在屏幕的右下角。

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="32dp"
    android:layout_marginRight="32dp"
    android:layout_marginBottom="32dp"
    android:src="@drawable/ic_add_alarm_black_24dp"
    android:background="@color/colorAccent"
    app:backgroundTint="@color/colorAccent"
    app:fabSize="normal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent" />
英文:

First if you use AndroidX be sure to include the following dependency to your app gradle.

implementation &#39;com.google.android.material:material:1.0.0&#39;

Next replace your FAB in the XML with the code below. The FAB should appear in the bottom-right corner of the screen.

    &lt;com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id=&quot;@+id/fab&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginEnd=&quot;32dp&quot;
        android:layout_marginRight=&quot;32dp&quot;
        android:layout_marginBottom=&quot;32dp&quot;
        android:src=&quot;@drawable/ic_add_alarm_black_24dp&quot;
        android:background=&quot;@color/colorAccent&quot;
        app:backgroundTint=&quot;@color/colorAccent&quot;
        app:fabSize=&quot;normal&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot; /&gt;

答案2

得分: 0

尝试在XML中使用以下代码:

app:backgroundTint="@color/red"
英文:

try this n the xml:

app:backgroundTint=&quot;@color/red&quot;

huangapple
  • 本文由 发表于 2020年1月6日 21:16:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定