【发布时间】:2021-12-07 05:59:09
【问题描述】:
有人可以帮我弄清楚为什么按钮的 backgroundTint 不起作用吗?我使用android:background 作为背景颜色,使用android:backgroundTint 作为按钮背景。但似乎 BackgroundTint 不起作用(按钮背景颜色设置为默认值)。
<?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=".MainActivity">
<TextView
android:id="@+id/tvCounter"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/default_background"
android:gravity="center"
android:text="0"
android:textColor="#FFFFFF"
android:textSize="200sp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tvCounter">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="@color/black"
android:background="@color/black"
android:onClick="gantiBackground"
android:text="HITAM"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="@color/red"
android:background="@color/red"
android:onClick="gantiBackground"
android:text="MERAH"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="@color/blue"
android:background="@color/blue"
android:onClick="gantiBackground"
android:text="BIRU"
android:textColor="#FFFFFF"/>
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:backgroundTint="@color/green"
android:background="@color/green"
android:onClick="gantiBackground"
android:text="HIJAU"
android:textColor="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="tambahCounter"
android:text="TAMBAH"/>
<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="reset"
android:text="RESET"/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
【问题讨论】: