如何在不使用外部库的情况下打印带有上标或下标的字符串?我希望将其显示在Android的TextView
中。
如何在不使用外部库的情况下打印带有上标或下标的字符串?我希望将其显示在Android的TextView
中。
示例:
equation = (TextView) findViewById(R.id.textView1);
SpannableStringBuilder cs = new SpannableStringBuilder("X3 + X2");
cs.setSpan(new SuperscriptSpan(), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cs.setSpan(new RelativeSizeSpan(0.75f), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cs.setSpan(new SuperscriptSpan(), 6, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cs.setSpan(new RelativeSizeSpan(0.75f), 6, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
equation.setText(cs);
equation.setText(blah+cs);
时,它不起作用。但是分开使用却可以正常工作。如何让它起作用? - Nobody对于所有询问如何缩小字体的人,除了使用上标或下标之外,你只需要添加small
标签即可。例如:
"X <sup><small> 2 </small></sup>"
在代码中添加"\u00B2",像这样:
textView.setText("X\u00B2");
虽然有些晚了,但以下内容仍然有效,使用上标作为特殊字符,在此我使用了特殊字符。
<string name="str">H₂</string>
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup><small>2</small></sup>"));
(或)来自字符串资源文件:
<string name="test_string">
<![CDATA[ X<sup><small>2</small></sup> ]]>
</string>
已接受的答案现在已经过时了,请查看这段代码。我从某个网站上找到了这段代码,但我忘记了这个网站的名字,无论如何感谢这段有效的工作代码。
SpannableString styledString
= new SpannableString("Large\n\n" // index 0 - 5
+ "Bold\n\n" // index 7 - 11
+ "Underlined\n\n" // index 13 - 23
+ "Italic\n\n" // index 25 - 31
+ "Strikethrough\n\n" // index 33 - 46
+ "Colored\n\n" // index 48 - 55
+ "Highlighted\n\n" // index 57 - 68
+ "K Superscript\n\n" // "Superscript" index 72 - 83
+ "K Subscript\n\n" // "Subscript" index 87 - 96
+ "Url\n\n" // index 98 - 101
+ "Clickable\n\n"); // index 103 - 112
// make the text twice as large
styledString.setSpan(new RelativeSizeSpan(2f), 0, 5, 0);
// make text bold
styledString.setSpan(new StyleSpan(Typeface.BOLD), 7, 11, 0);
// underline text
styledString.setSpan(new UnderlineSpan(), 13, 23, 0);
// make text italic
styledString.setSpan(new StyleSpan(Typeface.ITALIC), 25, 31, 0);
styledString.setSpan(new StrikethroughSpan(), 33, 46, 0);
// change text color
styledString.setSpan(new ForegroundColorSpan(Color.GREEN), 48, 55, 0);
// highlight text
styledString.setSpan(new BackgroundColorSpan(Color.CYAN), 57, 68, 0);
// superscript
styledString.setSpan(new SuperscriptSpan(), 72, 83, 0);
// make the superscript text smaller
styledString.setSpan(new RelativeSizeSpan(0.5f), 72, 83, 0);
// subscript
styledString.setSpan(new SubscriptSpan(), 87, 96, 0);
// make the subscript text smaller
styledString.setSpan(new RelativeSizeSpan(0.5f), 87, 96, 0);
// url
styledString.setSpan(new URLSpan("http://www.google.com"), 98, 101, 0);
// clickable text
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
// We display a Toast. You could do anything you want here.
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
};
styledString.setSpan(clickableSpan, 103, 112, 0);
// Give the styled string to a TextView
spantext = (TextView) findViewById(R.id.spantext);
// this step is mandated for the url and clickable styles.
spantext.setMovementMethod(LinkMovementMethod.getInstance());
// make it neat
spantext.setGravity(Gravity.CENTER);
spantext.setBackgroundColor(Color.WHITE);
spantext.setText(styledString);
注意:始终将android:textAllCaps="false"
放在您的spantext中。
如果您想从string.xml文件设置上标,请尝试以下方法:
字符串资源:
<string name="test_string">X<sup>3</sup></string>
如果您想让上标更小:
<string name="test_string">X<sup><small>3</small></sup></string>
代码:
textView.setText(Html.fromHtml("Anything you want to put here"+getString(R.string.test_string)));
对于“a”,请复制并粘贴“ᵃ”
您可以直接复制并粘贴任何这些上标和下标到您的Android字符串资源中。
例如:
<string name="word_with_superscript" translatable="false">Trademark ᵀᴹ</string>
结果: 商标 ᵀᴹ
上标和下标字母
上标大写字母 ᴬ ᴮ ᴰ ᴱ ᴳ ᴴ ᴵ ᴶ ᴷ ᴸ ᴹ ᴺ ᴼ ᴾ ᴿ ᵀ ᵁ ⱽ ᵂ
上标小写字母 ᵃ ᵇ ᶜ ᵈ ᵉ ᶠ ᵍ ʰ ⁱ ʲ ᵏ ˡ ᵐ ⁿ ᵒ ᵖ ʳ ˢ ᵗ ᵘ ᵛ ʷ ˣ ʸ ᶻ
下标小写字母 ₐ ₑ ₕ ᵢ ⱼ ₖ ₗ ₘ ₙ ₒ ₚ ᵣ ₛ ₜ ᵤ ᵥ ₓ
HTML.fromHTML(String)在API 24中已经被弃用。他们建议使用支持flags参数的新方法。因此,根据已接受的答案:
TextView textView = ((TextView)findViewById(R.id.text));
textView.setText(Html.fromHtml("X<sup>2</sup>", Html.FROM_HTML_MODE_LEGACY));
如果您需要考虑早于Android 4.4的API,可以使用以下代码:
TextView textView = ((TextView)findViewById(R.id.text));
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml("X<sup>2</sup>", Html.FROM_HTML_MODE_LEGACY));
} else {
textView.setText(Html.fromHtml("X<sup>2</sup>"));
}