英文:
FloatingActionButton with RecyclerView
问题
我的FloatingActionButton
呈灰色。我尝试使用CoordinatorLayout
,但它不起作用。以下是问题的屏幕截图和代码片段。
<?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.
<?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>
答案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 'com.google.android.material:material:1.0.0'
Next replace your FAB in the XML with the code below. The FAB should appear in the bottom-right corner of the screen.
<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" />
答案2
得分: 0
尝试在XML中使用以下代码:
app:backgroundTint="@color/red"
英文:
try this n the xml:
app:backgroundTint="@color/red"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论