无法使用 Html.fromHtml() 为 Android 按钮文本创建上标
Unable to create superscript for Android Button text with Html.fromHtml()
我已经通读了 these questions,它们似乎有我正在努力解决的相同问题,即我有我的按钮的文本,我想将其显示为上标。在我的 class 片段 onCreateView
方法中,我添加了
if (rootView.findViewById(R.id.squared) != null) {
((TextView) rootView.findViewById(R.id.squared)).setText(Html.fromHtml("X <sup><small> 2 </small></sup>"));
rootView.findViewById(R.id.squared).setOnClickListener(this);
}
if (rootView.findViewById(R.id.cubed) != null) {
((TextView) rootView.findViewById(R.id.cubed)).setText(Html.fromHtml("X <sup><small> 3 </small></sup>"));
rootView.findViewById(R.id.cubed).setOnClickListener(this);
在我的 fragment_main
中,按钮编码为
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:fontFamily="helvetica"
android:id="@+id/squared"
android:layout_weight="0.25"
style="?android:attr/borderlessButtonStyle"
android:textSize="15sp" />
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:fontFamily="helvetica"
android:id="@+id/cubed"
android:layout_weight="0.25"
style="?android:attr/borderlessButtonStyle"
android:textSize="15sp" />
但是,当我 运行 我的应用程序布局没有任何上标文本。
即使我将文本大小从 15sp
更改为 10sp
,文本只会变小,但不会上标。我做错了什么?
如果可以的话,我会发表评论,但现在我不能。我刚刚发布了与此类似的内容,我也遇到了这个问题。我的问题中途解决了。 Html.fromHtml 方法仅适用于文本视图,但不适用于我的按键。我只是想这样说,以防它可以帮助您或其他任何人了解如何在按钮上使用此方法。我自己也很想回答。
基本上:
textview.setText(Html.fromHtml("x<sup>2</sup"));
正常显示
但 button.setText(Html.fromHtml("x<sup>2</sup"));
显示 x2(无上标)。
编辑:AlanV 帮助了我。这是他为其他人提供的解决方案的 link。
您只需将此添加到您的按钮即可:
<button
android:textAllCaps="false";/>
新版本 5.0(我认为)强制按钮生成所有大写字母。当我在 textview 和按钮中生成相同的字符串时,我注意到了这一点,并且按钮全部大写而 textview 不是。因此,您看不到 HTML 格式,例如 and 。将 textAllCaps 功能设置为 "false" 允许使用 HTML。
再次强调,AlanV 就是那个人! :-)
这是一个已知问题,如图 here。
我觉得最好的办法就是设置 android:textAllCaps="false" ,然后自己设置文字大写.
我已经通读了 these questions,它们似乎有我正在努力解决的相同问题,即我有我的按钮的文本,我想将其显示为上标。在我的 class 片段 onCreateView
方法中,我添加了
if (rootView.findViewById(R.id.squared) != null) {
((TextView) rootView.findViewById(R.id.squared)).setText(Html.fromHtml("X <sup><small> 2 </small></sup>"));
rootView.findViewById(R.id.squared).setOnClickListener(this);
}
if (rootView.findViewById(R.id.cubed) != null) {
((TextView) rootView.findViewById(R.id.cubed)).setText(Html.fromHtml("X <sup><small> 3 </small></sup>"));
rootView.findViewById(R.id.cubed).setOnClickListener(this);
在我的 fragment_main
中,按钮编码为
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:fontFamily="helvetica"
android:id="@+id/squared"
android:layout_weight="0.25"
style="?android:attr/borderlessButtonStyle"
android:textSize="15sp" />
<Button
android:layout_height="match_parent"
android:layout_width="0dp"
android:fontFamily="helvetica"
android:id="@+id/cubed"
android:layout_weight="0.25"
style="?android:attr/borderlessButtonStyle"
android:textSize="15sp" />
但是,当我 运行 我的应用程序布局没有任何上标文本。
即使我将文本大小从 15sp
更改为 10sp
,文本只会变小,但不会上标。我做错了什么?
如果可以的话,我会发表评论,但现在我不能。我刚刚发布了与此类似的内容,我也遇到了这个问题。我的问题中途解决了。 Html.fromHtml 方法仅适用于文本视图,但不适用于我的按键。我只是想这样说,以防它可以帮助您或其他任何人了解如何在按钮上使用此方法。我自己也很想回答。
基本上:
textview.setText(Html.fromHtml("x<sup>2</sup"));
正常显示
但 button.setText(Html.fromHtml("x<sup>2</sup"));
显示 x2(无上标)。
编辑:AlanV 帮助了我。这是他为其他人提供的解决方案的 link。
您只需将此添加到您的按钮即可:
<button
android:textAllCaps="false";/>
新版本 5.0(我认为)强制按钮生成所有大写字母。当我在 textview 和按钮中生成相同的字符串时,我注意到了这一点,并且按钮全部大写而 textview 不是。因此,您看不到 HTML 格式,例如 and 。将 textAllCaps 功能设置为 "false" 允许使用 HTML。
再次强调,AlanV 就是那个人! :-)
这是一个已知问题,如图 here。
我觉得最好的办法就是设置 android:textAllCaps="false" ,然后自己设置文字大写.