【问题标题】:TextInputLayout not showing HTML styled textTextInputLayout 不显示 HTML 样式的文本
【发布时间】:2015-07-24 07:48:23
【问题描述】:

我有一些在片段中使用的 TextInputLayout。现在,对于其中一些的提示,我需要显示下标或上标(例如 gn 或 s2)。当我在字符串资源中使用这些标签时,它们不会显示。我的资源如下所示:

<string name="some_string">meters<sup>2</sup></string>

关于 TextInputLayout,我尝试在 XML 和编程方式中设置提示,但没有成功,如下所示:

<android.support.design.widget.TextInputLayout
    android:id="@+id/textInputLayout_example"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <EditText
            android:id="@+id/editText_example"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:digits="0123456789."
            android:selectAllOnFocus="true"
            android:ems="10"
            android:singleLine="true"
            android:maxLines="1"
            android:scrollHorizontally="true"
            android:hint="@string/some_string"/>

</android.support.design.widget.TextInputLayout>

TextInputLayout mTextInputLayout = (TextInputLayout) rootView.findViewById(R.id.textInputLayout_example);
mTextInputLayout.setHint(Html.fromHtml(getString(R.string.some_string)));

现在,我知道在开发人员指南中只提到支持&lt;b&gt;, &lt;i&gt;, and &lt;u&gt;,但查看https://github.com/android/platform_frameworks_base/blob/lollipop-mr1-release/core/java/android/content/res/StringBlock.java#L166 你会发现支持更多的标签。

话虽如此,似乎没有任何标签在工作。例如,如果我使用&lt;i&gt; 标记,则文本不是斜体。此外,我尝试在纯 TextView 上使用相同的字符串资源,但它在那里也不起作用。一开始我以为只是M-DP2的问题,于是在KitKat模拟器上试了一下,还是不行。

有人有什么想法吗?

【问题讨论】:

  • 我的解决方案有效吗?

标签: android material-design android-design-library


【解决方案1】:

我遇到了类似的问题。我通过在设置文本而不是在 xml 中执行 &lt;sup&gt; 来修复它。

.setText(Html.fromHtml(mContext.getString(R.string.YOUR_STRING) + 
"<sup><small>2</small></sup>"));

【讨论】:

  • 这在 TextView 和纯 EditText 上工作得很好。但是,它不适用于包含在 TextInputLayout 中的 EditText。嗯,我想知道这是否是 TextInputLayout 的错误……谢谢您的建议。它帮助我缩小了问题的范围。
猜你喜欢
  • 2017-03-06
  • 2020-01-08
  • 2015-08-21
  • 2012-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-07
  • 2011-08-14
相关资源
最近更新 更多