TextView 的背景没有改变

问题描述

我已经搜索了 SO,但我发现的任何内容都无法解决此问题。我正在尝试使用数据绑定根据视图中的文本更改 TextView 的可绘制背景。

TextView.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">
    <data>

       <variable name="data" type="package.name.Exercise" />

       <variable name="playTime" type="org.joda.time.Instant" />

       <import type="package.name.util.Converter" />
       <import type="android.view.View"/>
       <import type="package.name.R" />
    </data>

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="16dp"
        android:layout_marginVertical="8dp"
        android:contentDescription='@{data.title.localized() + "," + data.description.localized()}'
        app:accClickHint="@{@string/acc_exercises_hint}"
        app:cardCornerRadius="8dp"
        app:cardElevation="2dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/icon"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:layout_marginStart="10dp"
                android:layout_marginTop="30dp"
                android:importantForAccessibility="no"
                android:scaleType="fitCenter"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@id/title"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@{data.icon}"
                tools:srcCompat="@sample/exercise_icon" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:gravity="right">

                        <TextView
                            android:id="@+id/capacity1"
                            android:layout_width="75dp"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="7.5dp"
                            android:layout_marginBottom="4dp"
                            android:gravity="center"
                            android:textAppearance="@style/Small"
                            android:text="@{data.capacity.get(0).title()}"
                            android:background="@{data.capacity.get(0).color()}"/>

                      <!--More views here...-->

TextView.xml 有更多内容,但不会影响 TextView。 data.capacity.get(0).color() 来自于 Exercise.java。我使用了断点并且正在返回可绘制对象。

练习.java

public abstract class Exercise implements ExerciseListObject {

    public enum Capacity {
        THINK,FEEL,CTRL,EMO;

        public String title() {
            switch (this) {
                case THINK:
                    return "Cognition";
                case FEEL:
                    return "Feeling";
                case CTRL:
                    return "Self Control";
                case EMO:
                    return "Emotion";
            }
            throw new RuntimeException("Not a recognized case: " + this);
        }

        public int color() {
            switch (this) {
                case FEEL:
                    return R.drawable.shape_rounded_rectangle_4px_feeling;
                case THINK:
                    return R.drawable.shape_rounded_rectangle_4px_cognition;
                case CTRL:
                    return R.drawable.shape_rounded_rectangle_4px_control;
                case EMO:
                    return R.drawable.shape_rounded_rectangle_4px_emo;
            }
            throw new RuntimeException("Not a recognized case: " + this);
        }
    }
    
    // More irrelevant code here ...
}

shape_rounded_rectangle_4px_feeling.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
        android:bottomLeftRadius="4dp"
        android:bottomRightRadius="4dp"
        android:topLeftRadius="4dp"
        android:topRightRadius="4dp" />

    <solid android:color="@color/feelingAccordion" />

</shape>

问题是它不会正确设置背景。它设置了一个看起来像这样的背景:

enter image description here

背景应该是这种颜色:

enter image description here

知道发生了什么吗?提前致谢!

解决方法

多亏了这个网站,我才明白:https://medium.com/swlh/android-data-binding-in-views-for-theming-your-app-580c0bf06c18

我需要将此方法放在数据绑定适配器文件中。

@BindingAdapter("android:background")
public static void setTextViewBackground(TextView view,int resource){
    view.setBackgroundResource(resource);
}

没有这个方法,数据绑定库不知道如何处理背景属性。

,

尝试 backgroundTint ,当您想更改背景颜色而不为此创建样式时可以解决您的问题,但如果您的问题没有解决,请标记我以另一种方式告诉您

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...