代码之家  ›  专栏  ›  技术社区  ›  Vikalp Patel

使用数据绑定使文本的某些部分加粗

  •  1
  • Vikalp Patel  · 技术社区  · 6 年前

    例如

    如果您被选中,您将支付 为了你的那一对。

    我正在使用字符串资源

    <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())

    我知道以下解决方案:但是想尝试使用数据绑定

    • 使用Html文本-但不想在代码中使用它。
    • 在水平样式中使用不同的文本视图。为产品价格设置粗体样式。-真是糟糕的做法

    但上述所有解决方案都是权宜之计。

    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

    4 回复  |  直到 6 年前
        1
  •  2
  •   rbd_sqrl    6 年前

    可以使用绑定适配器和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 }"/>
    

    祝你好运。

        2
  •  2
  •   Mahavir Jain    6 年前

    你必须创建一个 BindingAdapter SpannableStringBuilder .

    绑定适配器

    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
  •  1
  •   Vikalp Patel    6 年前

    根据@commonware,

    <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>
    
        4
  •  0
  •   Dhara Jani    6 年前

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

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

    //在xml中,userList是模型类的变量名。

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