在Android中显示上标和特殊符号

时间:2018-01-29 03:09:59

标签: android android-layout android-studio

我正在使用

private final static String MICRO = "&#x3BC";

private final static String SQUARE = "&#xB2";

将ms ^ 2和MICRO符号显示为

case Sensor.TYPE_ACCELEROMETER:
    units = "m/s" + SQUARE;
case Sensor.TYPE_MAGNETIC_FIELD:
    units = MICRO + "T";

但它返回相同的值。即MICRO = "&#x3BC"; and SQUARE = "&#xB2";在App上。有没有其他方式来显示这些单位?

2 个答案:

答案 0 :(得分:0)

只需更改MICRO = "&#x3BC"; to MICRO = "μ";SQUARE = "&#xB2"; to SQUARE = "²";有效

答案 1 :(得分:0)

这是另一个选项 - 使用HTML。

首先,创建XML字符串

<string name="ms"><![CDATA[%1$s m/s<sup><small><small>2</small></small><sup>]]></string>
<string name="ut"><![CDATA[%1$s &mu;T]]></string>

现在,在Java中使用它们:

TextView txtMs = findViewById(R.id.txtMs);
String ms2 = String.format(getResources().getString(R.string.ms), "2.8");
txtMs.setText(Html.fromHtml(ms2));

TextView txtUt = findViewById(R.id.txtUt);
String ut = String.format(getResources().getString(R.string.ut), "2000.0");
txtUt.setText(Html.fromHtml(ut));

结果:

enter image description here