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 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}"