TextView 的背景没有改变

如何解决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 ,当您想更改背景颜色而不为此创建样式时可以解决您的问题,但如果您的问题没有解决,请标记我以另一种方式告诉您

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?