【问题标题】:Setting superscript text for android button为android按钮设置上标文本
【发布时间】:2016-10-15 13:32:11
【问题描述】:

我不知道如何在 android 按钮中设置上标文本(用于数学运算符)。

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

Xy

我希望 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 类中?

【问题讨论】:

标签: android android-studio button superscript


【解决方案1】:

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

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

然后在您的活动中访问它-

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

这将有助于获得 x^2

【讨论】:

  • 问题不在于setText(Html.fromHtml("X &lt;sup&gt;&lt;small&gt; y &lt;/small&gt;&lt;/sup&gt;")); 不起作用,而在于rootView 无法解析,因此代码甚至达不到那么远。
【解决方案2】:

在 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>"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2011-04-06
    相关资源
    最近更新 更多