Java 文本视图的背景未更改
我已经搜索了这么多,我发现没有任何东西能够解决这个问题。我正在尝试使用数据绑定根据视图中的文本更改TextView的背景可绘制性 TextView.xmlJava 文本视图的背景未更改,java,android,textview,android-drawable,android-databinding,Java,Android,Textview,Android Drawable,Android Databinding,我已经搜索了这么多,我发现没有任何东西能够解决这个问题。我正在尝试使用数据绑定根据视图中的文本更改TextView的背景可绘制性 TextView.xml <?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://
<?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...-->
<?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>
shape\u round\u rectangle\u 4px\u feeling.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...-->
<?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>
问题是它无法正确设置背景。它设置的背景如下所示:
背景应为以下颜色:
知道发生了什么吗?提前谢谢 多亏了这个网站,我才明白这一点: 我需要将此方法放入数据绑定适配器文件中
@BindingAdapter("android:background")
public static void setTextViewBackground(TextView view, int resource){
view.setBackgroundResource(resource);
}
如果没有此方法,数据绑定库将不知道如何处理background属性。请尝试
backgroundTint
,如果您想更改背景颜色而不创建样式,则可以使用此选项解决问题,但如果您的问题没有解决,请标记我以另一种方式告诉您Lol这些背景在我看来是一样的@DavidKroukamp我正在编辑并选择了错误的图片。虽然正常情况下这会起作用,但在这种情况下没有帮助。我找到了答案,并发布了我的具体问题的答案。谢谢你的回答!欢迎你,我的兄弟对不起,我帮不了你@恩德史密斯