无法使用Html.fromHtml()为Android Button文本创建上标

idc*_*ark 4 formatting android android-layout

我已经阅读了这两个问题,这些 问题似乎与我正在努力解决的问题相同,即我的按钮文本我想显示为上标.在我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);
Run Code Online (Sandbox Code Playgroud)

在我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" />
Run Code Online (Sandbox Code Playgroud)

但是,当我运行我的应用程序时,布局不会有任何上标文本.

在此输入图像描述

即使我将文本大小更改15sp10sp,文本也只会变小,但不会上标.我做得不好?

lil*_*win 16

如果可以,我会评论,但此时我不能.我刚发布了类似的东西,我也有一个问题.我中途解决了我的问题.Html.fromHtml方法仅适用于textview,但不适用于我的butons.我只是想说这个,以防它或其他任何人想出如何在按钮上使用这种方法.我非常喜欢这个答案.

基本上: textview.setText(Html.fromHtml("x<sup>2</sup"));正确显示

但是button.setText(Html.fromHtml("x<sup>2</sup"));显示x2(没有上标).

编辑:AlanV帮助了我.这是他向其他人提供的解决方案的链接./sf/answers/2033975681/

您需要做的就是将其添加到按钮:

<button
    android:textAllCaps="false";/>
Run Code Online (Sandbox Code Playgroud)

新版本5.0(我认为)强制按钮生成所有大写字母.当我在textview和按钮中生成相同的字符串时,我注意到这一点,并且按钮全部为大写,而textview则不是.因此,您不会看到HTML格式,例如和.将textAllCaps功能设置为"false"允许使用HTML.

再一次,给予应有的信用,AlanV就是男人!:-)