Android Studio 设计时数据绑定回退/默认值
我正在使用 Android 数据绑定,效果很好:
I am using Android data binding which works great:
<TextView
android:text="@{ee.Name}"
...
但如果我这样做,Android Studio 设计器不会显示任何文本.没有文字,我根本看不到 TextView.这是可以理解的,因为我还没有绑定数据.有没有像后备值或默认值这样的东西可以显示,直到有真实数据?
But if I do that the Android Studio designer doesn't show any text. Without text I can't see the TextView at all. Which is understandable because I haven't bound the data yet. Is there something like a fallback value or a default value which can be displayed until there is real data?
您应该阅读 数据绑定指南 发布在 Android 开发者网站上.文档的最后一部分,Android Studio 对数据绑定的支持解释如何使用可以在设计阶段为您提供帮助的占位符.很简单:
You should read the Data Binding Guide posted on the Android developers website. The last section of the document, Android Studio Support for Data Binding explain how you can use a placeholder that can help you during the design phase. It's very simple:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.firstName, default=Placeholder}"/>
如果您想将带有空格的文本用作占位符,您可以使用单引号 (')、反引号 (`) 或 "
If you want to have text with spaces as placeholder you can use single quotes ('), back quotes (`) or "
android:text='@{user.firstName, default="Placeholder text"}'
android:text="@{user.firstName, default=`Placeholder text`}"
android:text="@{user.firstName, default="Placeholder text"}"
android:text="@{user.firstName, default=@string/placeholder_text}"