【问题标题】:How to make bold text with Android Data Binding Library如何使用 Android 数据绑定库制作粗体文本
【发布时间】:2016-10-03 05:19:41
【问题描述】:

非常基本,我想根据是否阅读的文本将消息的标题设为粗体。我似乎找不到解决办法。

这是我的 XML 代码:

            <TextView
                android:text="@{message.title}"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginBottom="5dp"
                android:layout_marginTop="5dp"
                android:layout_toLeftOf="@+id/timestamp"
                android:textSize="18sp"
                android:textStyle='@{message.isRead() ? "bold" : "normal"}'
                android:textColor='@{message.isRead() ? 0xff313131 : 0xff0662ab}' />

换色效果很好,只有粗体字给我带来了一些问题。

错误:任务 ':app:compileDebugJavaWithJavac' 执行失败。

java.lang.RuntimeException:发现数据绑定错误。 ****/ 数据绑定错误 ****msg:在 android.widget.TextView 上找不到参数类型为 java.lang.String 的属性“android:textStyle”的设置器。 文件:D:......xml 地点:39:41 - 39:79 ****\数据绑定错误****

【问题讨论】:

  • 如果您只是静态设置粗体文本,而不是数据绑定,它是否有效?
  • 是的,然后就可以了
  • 错误中编辑错误

标签: android android-databinding


【解决方案1】:

所以,本周我遇到了同样的问题,我尝试使用view.setTypeface() 方法,但我想在我的情况下保留字体样式。事实证明,当我在 setTypeFace 方法中传递 null 时,粗体样式有效,但是...传递 null 可以更改字体样式(看起来不一样)。当我尝试使用相同的字体样式并将视图设置为粗体时,它不起作用。

为了达到我想要的效果,由于我没有成功使用这种方法,所以我只是在需要加粗时更改字体样式,请参阅我的代码:

@BindingAdapter("boldOrRegular")
@JvmStatic
    fun TextView.boldOrRegular(isBold: Boolean) {
        typeface = if (isBold) {
            ResourcesCompat.getFont(context, R.font.myfont_bold)
        } else {
            ResourcesCompat.getFont(context, R.font.myfont_regular)
        }
    }

我希望这可以帮助某人:)

【讨论】:

    【解决方案2】:

    您可以在不创建适配器的情况下执行此操作。

    将字体导入您的 XML

    <data>
        <import type="android.graphics.Typeface" />
    ...
    </data>
    

    使用属性android:typefaceTypeface.defaultFromStyle

    android:typeface="@{Typeface.defaultFromStyle(message.isRead() ? Typeface.BOLD : Typeface.NORMAL)}"
    

    【讨论】:

      【解决方案3】:

      您只需将字体导入 xml 文件并像这样进行检查

          <variable
              name="vm"
              type="com.example.VM" />
      
          <import type="android.graphics.Typeface" />
      
      
              <TextView
                  android:id="@+id/username"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textStyle="@{vm.isBold ? Typeface.BOLD : Typeface.NORMAL}"/>
      

      【讨论】:

      • 对我不起作用。无法找到接受参数类型“int”的 的设置器。看来它毕竟需要一个自定义适配器。
      【解决方案4】:

      简单的方法

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

      XML:

          <TextView
              android:id="@+id/text"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:isBold="@{item.bold}"/>
      

      【讨论】:

      • 这是正确的解决方案,接受的答案已过时。
      【解决方案5】:

      Saif Bechan 的回答是正确的,我做了一些小改动以促进视图模型的数据绑定。

      public abstract class BindingAdapter {
          @android.databinding.BindingAdapter("app:textStyle")
          public static void setTextStyle(TextView v, int style) {
              v.setTypeface(null, style);
          }
      }
      

      然后将 app 命名空间引入 XML

      <layout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto">
      ...
      </layout>
      

      在视图模型中创建绑定

      @Bindable
      public int getValueFormat() {
          String message = getMyObject().getValue();
          if (message == MyObject.DEFAULT_VALUE)
              return Typeface.ITALIC;
      
          return Typeface.NORMAL;
      }
      

      现在可以直接绑定了

      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:textStyle="@{viewModel.valueFormat}"
          android:text="@{viewModel.value}" />
      

      【讨论】:

        【解决方案6】:

        我最终使用了以下代码,它实现了 DataBinding。

        public abstract class BindingAdapter {
            @android.databinding.BindingAdapter("android:typeface")
            public static void setTypeface(TextView v, String style) {
                switch (style) {
                    case "bold":
                        v.setTypeface(null, Typeface.BOLD);
                        break;
                    default:
                        v.setTypeface(null, Typeface.NORMAL);
                        break;
                }
            }
        }
        

        还有 XML

        <TextView
            android:text="@{bericht.titel}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_toLeftOf="@+id/timestamp"
            android:textSize="18sp"
            android:textColor='@{bericht.isGelezen() ? 0xff313131 : 0xff0662ab}'
            android:typeface='@{bericht.isGelezen() ? "normal" : "bold"}' />
        

        【讨论】:

        • 这是正确的解决方案,但它可以使用一个小的调整:与 android:textStyle 绑定并在您的 XML 中使用 textStyle 属性。
        • 我把这个静态方法保存在 viewmodel 类 public abstract static class BindingAdapter { @android.databinding.BindingAdapter("android:typeface") public static void setTypeface(TextView v, String style) { switch (style ) { case "bold": v.setTypeface(null, Typeface.BOLD);休息;默认值:v.setTypeface(null, Typeface.NORMAL);休息; } } } 并进入 xml android:typeface='@{bericht.isGelezen() ? “正常”:“粗体”}'
        【解决方案7】:

        您需要在您的消息对象中创建getMessageStyle

        public String getMessageStyle() {
            return isRead() ? "bold" : "normal";
        }
        

        然后在您的代码中使用如下:

        <TextView
            android:text="@{message.title}"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_toLeftOf="@+id/timestamp"
            android:textSize="18sp"
            android:textStyle="@{message.getMessageStyle()}"
            android:textColor="@{message.isRead() ? 0xff313131 : 0xff0662ab}" />
        

        【讨论】:

        • 我试过了,这不起作用。我找到了另一种方法。我会尽快发布我的答案。
        猜你喜欢
        • 2014-07-13
        • 2014-10-04
        • 1970-01-01
        • 2015-11-20
        • 1970-01-01
        • 2022-11-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多