【问题标题】:android:backgroundTint isn't working - Androidandroid:backgroundTint 不工作 - Android
【发布时间】: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>

【问题讨论】:

    标签: android xml


    【解决方案1】:

    使用 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>
        
    

    【讨论】:

      【解决方案2】:

      使用

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

      代替

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

      为“background”和“backgroundTint”尝试不同的颜色

      你对“背景”和“背景色调”使用相同的颜色..这就是为什么“背景色调”看起来不起作用......

      【讨论】:

      • 还是不行
      • 您正在为该活动使用哪个主题...?
      • 设置@style/Theme.AppCompat.Light.NoActionBar 主题..
      猜你喜欢
      • 2015-10-06
      • 2015-01-04
      • 2018-03-26
      • 2012-08-14
      • 2014-04-18
      • 2011-11-26
      • 2016-01-02
      • 2016-09-09
      • 2015-05-02
      相关资源
      最近更新 更多