gpt4 book ai didi

android - 在布局 XML 中有条件地将 TextView textStyle 设置为斜体

转载 作者:行者123 更新时间:2023-12-02 13:16:26 25 4
gpt4 key购买 nike

我希望有条件地在我的布局文件中设置 TextView 的 textStyle 属性。直接设置“正常”或“斜体”可以正常工作,但我将如何根据数据绑定(bind)中的一些 bool 值应用这两者之一?

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="isComplete"
type="boolean" />
</data>

...

<TextView>
android:text="Some Text"
android:textStyle="@{isComplete ? @string/textStyle_normal : @string/textStyle_italic}"
</TextView>
当我尝试运行上述内容时,错误不清楚,但我的 XXXBindingImpl 类似乎没有生成。我可以在 kotlin 中以编程方式更新文本样式,但我很好奇是否有办法让它在 View 本身中工作。

最佳答案

您可以使用绑定(bind)适配器来实现这一点:
BindingAdapter.kt

@JvmStatic
@BindingAdapter("setTextCustomStyle")
fun TextView.setTextCustomStyle(isNormal:Boolean){
if (isNormal) this.setTypeface(this.typeface,Typeface.NORMAL) else this.setTypeface(this.typeface,Typeface.ITALIC)
}
xyz.xml:
        <TextView
android:id="@+id/tvItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{item.title}"
setTextCustomStyle="@{isComplete}"
tools:text="some text"/>

关于android - 在布局 XML 中有条件地将 TextView textStyle 设置为斜体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63731049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com