如何在 TextView 中设置默认文本以在绑定时在布局预览中看到它?

How to set default text in TextView to see it in the layout preview while binding?

所以我希望看到我的布局预览,其中的字段填充了默认占位符之类的东西,但是如果我使用绑定,settext 属性已经被使用并且字段显示为空,因为还没有来自模型的信息。

<TextView
                android:id="@+id/tv_user_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_margin="5dp"
                android:gravity="center"
                **android:text="@{showBlueportSpotViewModel.name}"**
                android:textAllCaps="true"
                android:textSize="20sp"
                android:textStyle="bold"/>

我试过这个:

android:text="@{showBlueportSpotViewModel.name ?? @string/blueport_placeholder_name}"

但我仍然看到视图是空的。

你们有什么解决方法吗?我想一旦找到解决方法,它就可以用于 ImageView 和 src 等。

谢谢!

我找到了解决方法

我加了

xmlns:bind="http://schemas.android.com/apk/res/android"

到布局

并且只是在视图中有重复的声明,例如:

android:text="@string/blueport_placeholder_name"
bind:text="@{showBlueportSpotViewModel.name}"

android:src="@{showBlueportSpotViewModel.blueportImageDrawable}" bind:src="@drawable/android_menu_header"

我真的不知道这是否会产生次要的错误后果,所以我不会接受这个答案,直到有人发表评论并说它是否可以。谢谢!

您可以使用 tools 属性来定义将出现在布局预览中但不会在您 运行 应用程序时出现的属性。

将以下内容添加到您的根视图:

xmlns:tools="http://schemas.android.com/tools"

然后使用 tools 属性定义只会出现在布局预览中的文本:

tools:text="placeholder text"

在编辑器中模拟视图时,tools 属性非常有用。打包应用程序时,所有工具属性都将被剥离。更多信息在这里:http://tools.android.com/tech-docs/tools-attributes