为android按钮设置上标文本

时间:2016-10-15 13:32:12

标签: android android-studio button superscript

我无法弄清楚如何在Android按钮中设置上标文本(对于数学运算符)。

如下面的代码所示,我希望buttonXPow显示:

X ý

我希望button10Pow显示:

10 ý

我希望尽可能避免使用X^y10^y

我到目前为止找到的唯一可行解决方案是此代码(found in this stackoverflow question here):

protected void onCreateView() {

    if (rootView.findViewId(R.id.buttonXPow) !=null) {
        ((Button) rootView.findViewById(R.id.buttonXPow)).setText(Html.fromHtml("X <sup><small> y </small></sup>"));
    }
    if (rootView.findViewId(R.id.button10Pow) !=null) {
        ((Button) rootView.findViewById(R.id.button10Pow)).setText(Html.fromHtml("10 <sup><small> y </small></sup>"));
    }
}

问题在于无法解析rootView。我不知道如何让它工作(显然它不是我需要导入的类;我假设我需要以某种方式声明变量)。

是否有其他已知的设置上标文本的方法;也许用xml而不是主java类?

2 个答案:

答案 0 :(得分:1)

在strings.xml文件中添加以下内容 -

<string name="test_string"> <![CDATA[ X<sup><small>2</small></sup> ]]> </string>

然后在您的活动中使用 -

访问它

getResources().getString(R.string.test_string)

这将有助于获得x ^ 2

答案 1 :(得分:1)

将以下内容添加到onCreate方法中

((TextView)findViewById(R.id.buttonXPow)).setText(Html.fromHtml("X<sup>y</sup>"));
((TextView)findViewById(R.id.button10Pow)).setText(Html.fromHtml("10<sup>y</sup>"));