android:backgroundTint 不起作用 - Android



有人能帮我弄清楚为什么按钮的背景色不工作吗?我使用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>

使用app:backgroundTint代替android:backgroundTint

<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"
app:backgroundTint="@color/black"//edited line
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"
app:backgroundTint="@color/red"//edited line
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"
app:backgroundTint="@color/blue"//edited line
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"
app:backgroundTint="@color/green"//edited line
android:background="@color/green"
android:onClick="gantiBackground"
android:text="HIJAU"
android:textColor="#FFFFFF"/>
</LinearLayout>

使用

android:backgroundTint="@color/blue"
android:background="@color/red"

android:backgroundTint="@color/blue"
android:background="@color/blue"

尝试不同颜色的"背景";和"backgroundTint">

你的"背景"使用了相同的颜色和"backgroundTint". .这就是为什么&;backgroundcolor &;Look like不工作…

不能在较低版本的Android SDK中工作。豆形软糖)。

android:backgroundTint="@color/colorBlue"

使用

app:backgroundTint="@color/colorBlue"

将适用于所有Android sdk。

最新更新