使用DataBinding将文本的某些部分加粗

时间:2018-08-13 12:18:09

标签: android android-databinding android-mvvm

我想使文本的某些部分变为粗体,其值是通过使用ViewModel的DataBinding设置的。

例如

如果您被选中,则将为您的配对支付 160美元

我正在使用字符串资源

<string name="product_price">If you are selected, you will have to pay $%d for your pair.</string>

<TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginEnd="@dimen/spacing_xlarge"
          android:layout_marginStart="@dimen/spacing_xlarge"
          android:layout_marginBottom="@dimen/spacing_small"
          android:text="@{@string/product_price(productPrice)}"
          android:textColor="@color/button_tertiary"
          android:visibility="@{productPrice > 0}"
          style="@style/Body.Small"
          />

通过设置binding.setProductPrice(Object.getPrice()),使用带有绑定功能的ViewModel当前传递产品价格

我知道以下解决方案:但想尝试使用DataBinding

  • 使用HTML文本-但不想在代码中使用它。
  • 使用水平样式的其他TextView。将样式设置为该产品价格的粗体。 -实为不实做法
  • 使用SpannableString-但不想在代码中使用它。

但是上述所有解决方案都是解决方法。

问题::

Want to try DataBinding feature which can be used to style certain part of string. Just like SpannableString

Manipulate String in the Layout file using DataBinding

6 个答案:

答案 0 :(得分:0)

您可以将绑定适配器与SpannableString结合使用。定义绑定适配器后,就可以在所有布局文件中重复使用它。

@BindingAdapter({"mainText", "priceToFormat"})
public static void format(TextView textView, String mainText, float 
      productPrice){
   //Use spannable string to format your text accordingly
   textView.setText(formattedText);
}

您可以像这样在布局文件中传递这些参数:

<TextView
   .
   .
   app:mainText = "@{ priceText }"
   app:priceToFormat = "@{ price }"/>

祝你好运。

答案 1 :(得分:0)

///将此方法放在模型类中,其中名称是根据给定的api响应设置的字符串变量

public Spanned getHtmlText(){
        return Html.fromHtml("<b>" + name + "</b>");
    }
xml中的

//使用此方法,其中userList是模型类的变量名称。

 android:text="@{userList.htmlText}"

答案 2 :(得分:0)

您必须创建一个BindingAdapterSpannableStringBuilder

  

绑定适配器

object Util {
    @BindingAdapter("main","secondText")
        @JvmStatic
        fun setBoldString(view: AppCompatTextView, maintext: String,sequence: String) {
            view.text = Util.getBoldText(maintext, sequence)
        }



    @JvmStatic
        fun getBoldText(text: String, name: String): SpannableStringBuilder {
            val str = SpannableStringBuilder(text)
            val textPosition = text.indexOf(name)
            str.setSpan(android.text.style.StyleSpan(Typeface.BOLD),
                    textPosition, textPosition + name.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            return str
        }
}
  

XML

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:main="@{`you will pay $160 for your pair`}"
        app:secondText="@{`$160`}"
        android:textColor="@color/black"
        android:textSize="22sp" />

也许对您有帮助。

答案 3 :(得分:0)

按如下所述使用BindingAdapter

    @BindingAdapter("setBold")
    @JvmStatic
    public static void setBold(TextView view, boolean isBold) {
        if (isBold) {
            view.setTypeface(null, Typeface.BOLD);
        } else {
            view.setTypeface(null, Typeface.NORMAL);
        }
    }

并按如下所示在xml中使用它:

  <androidx.appcompat.widget.AppCompatTextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="@{model.value}"
        app:setBold="@{model.isBold}"
   />

答案 4 :(得分:0)

随着Html.fromHtml签名在API级别24(Android N)上的变化,Android团队引入了HtmlCompat,以便在任何api级别使用相同的签名。

因此,您应该使用HtmlCompat类:

HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY);

要使用它,您可以在项目的build.gradle中包括AndroidX核心:

implementation 'androidx.core:core:1.3.1'

布局XML文件:

<?xml version="1.0" encoding="utf-8"?>
 <layout
   <data>
   <import type="androidx.core.text.HtmlCompat"/>
   <data>
     <LinearLayout>
       <android.support.design.widget.CoordinatorLayout>
       <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginEnd="@dimen/spacing_xlarge"
          android:layout_marginStart="@dimen/spacing_xlarge"
          android:layout_marginBottom="@dimen/spacing_small"
          android:text="@{HtmlCompat.fromHtml(@string/product_price(productPrice),HtmlCompat.FROM_HTML_MODE_LEGACY)}"
          android:textColor="@color/button_tertiary"
          android:visibility="@{productPrice > 0}"
          style="@style/Body.Small"
          />
       </android.support.design.widget.CoordinatorLayout>
     </LinearLayout>
 </layout>

答案 5 :(得分:-1)

根据@CommonsWare,

尝试通过添加基本的HTML标签<string name="product_price">If you are selected, you will have to pay <![CDATA[<b>$%d</b>]]> for your pair.</string>

布局文件:导入的HTML

 <?xml version="1.0" encoding="utf-8"?>
 <layout
   <data>
   <import type="android.text.Html"/>
   <data>
     <LinearLayout>
       <android.support.design.widget.CoordinatorLayout>
       <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginEnd="@dimen/spacing_xlarge"
          android:layout_marginStart="@dimen/spacing_xlarge"
          android:layout_marginBottom="@dimen/spacing_small"
          android:text="@{Html.fromHtml(@string/product_price(productPrice))}"
          android:textColor="@color/button_tertiary"
          android:visibility="@{productPrice > 0}"
          style="@style/Body.Small"
          />
       </android.support.design.widget.CoordinatorLayout>
     </LinearLayout>
 </layout>
相关问题