作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开始使用新的数据绑定(bind) API。我想在 TextView
上绑定(bind)一个自定义属性,我可以在其中立即更改文本和背景。据我了解 api 有一个 @BindingMethod
注释,但文档有点薄弱。
我想要这样的东西:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Important"
tools:background="@drawable/bg_badge_important"
tools:textColor="#fff"
android:id="@+id/badge"
custom:badge="@{item.badge}"/>
这里 item.badge
是一个像 important 或 notice 这样的字符串。对于 i18n,我不能直接使用这个字符串,但这将帮助我选择正确的文本和正确的背景图像。
你能给我一个小例子或引用如何使用这个属性吗?我刚找到 one answer在 Stack Overflow 上,但这并没有回答我的问题。
最佳答案
只需在您的代码库上创建一个公共(public)静态方法并使用@BindingAdapter 进行注释即可。
这是一个字体示例:
https://plus.google.com/+LisaWrayZeitouni/posts/LTr5tX5M9mb
由 rekire 编辑我最终使用了这段代码:
@BindingAdapter({"bind:badge"})
public static void setBadge(TextView textView, String value) {
switch(value) {
case "email":
textView.setText(R.string.badge_email);
textView.setBackgroundResource(R.drawable.bg_badge_email);
break;
// other cases
}
}
关于android - 如何为 TextView 定义自定义 BindingMethod?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33714490/
我开始使用新的数据绑定(bind) API。我想在 TextView 上绑定(bind)一个自定义属性,我可以在其中立即更改文本和背景。据我了解 api 有一个 @BindingMethod注释,但文
我是一名优秀的程序员,十分优秀!