我试图多次使用带有include标签的具有数据绑定的视图。当应用运行时,它的效果很好,但是在预览中,我无法使其在父级中运行。取而代之的是,我可以在视图本身上执行此操作,但这意味着如果在同一布局中使用5次,则将有5个精确的副本。这不是一个阻塞性的问题,但是对于调试/ UI,这会使事情变得更加复杂。
这是复制视图的代码:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.view.View"/>
<import type="android.text.TextUtils"/>
<variable
name="title"
type="String" />
<variable
name="value"
type="String" />
</data>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="@{(!TextUtils.isEmpty(title) ? title : `Title`).concat(`:`), default=`Title:`}" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@{!TextUtils.isEmpty(value) ? @color/darkBlue : @color/red, default=@color/red }"
android:text="@{(!TextUtils.isEmpty(value) ? value : `Unknown`), default=`Unknown`}" />
</LinearLayout>
</layout>
这是它的包含方式:
<include layout="@layout/titled_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
bind:title="@{`Firstname`}"
bind:value="@{user.firstName}" />
But in preview it looks like this
有人知道如何解决它吗?
谢谢,在那些复杂的日子里过得愉快。
将您的字符串存储在string.xml中
并将其设置为默认值
如下所示
android:text='@{viewModel.firstName, default=@string/firstName}`