有人可以幫我弄清楚為什么按鈕的 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>
uj5u.com熱心網友回復:
用
android:backgroundTint="@color/blue"
android:background="@color/red"
代替
android:backgroundTint="@color/blue"
android:background="@color/blue"
為“background”和“backgroundTint”嘗試不同的顏色
你對“background”和“backgroundTint”使用相同的顏色......這就是為什么“backgroundTint”看起來不起作用......
uj5u.com熱心網友回復:
使用 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/331660.html
