【发布时间】: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