【问题标题】:Background of TextView not changingTextView的背景没有改变
【发布时间】:2021-01-28 17:40:56
【问题描述】:

我已经搜索过了,但我发现没有任何东西能够解决这个问题。我正在尝试使用数据绑定根据视图中的文本更改 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。我已经使用了断点并且正在返回可绘制对象。

Exercise.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>

问题是它不会正确设置背景。它设置了一个如下所示的背景:

背景应该是这种颜色:

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

【问题讨论】:

  • 哈哈,这些背景在我看来都一样????
  • @DavidKroukamp 我正在编辑并选择了错误的图片。

标签: java android textview android-drawable android-databinding


【解决方案1】:

感谢这个网站: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);
}

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 虽然这通常会起作用,但在这种情况下并没有帮助。我想通了,并发布了我的特定问题的答案。感谢您的回答!
    • 欢迎您,我的兄弟,对不起,我无法帮助您...@ndsmith
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 2015-07-15
    • 2013-08-10
    • 2021-12-22
    • 2019-09-18
    • 1970-01-01
    相关资源
    最近更新 更多