下付き文字または上付き文字を含む文字列を印刷するにはどうすればよいですか?外部ライブラリなしでこれを行うことはできますか?TextView
これをAndroidで表示したい。
15 に答える
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));
また
例:
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);
上付き文字や下付き文字を作成する以外に小さくしたい場合は、タグを追加するだけです。元:
"X <sup><small> 2 </small></sup>"
コードにこの「\u00B2」を入れてください。
textView.setText("X\u00B2");
少し遅れましたが、うまく機能した後、特殊文字として上付き文字を使用し、ここでは空間文字を使用しました。
<string name="str">H₂</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"
てください。
((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>
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)));
HTML.fromHTML(String)は、API 24で非推奨になりました。代わりに、これを使用すると言われています。これは、パラメーターとしてフラグをサポートします。だから、受け入れられた答えから抜け出すために:
TextView textView = ((TextView)findViewById(R.id.text));
textView.setText(Html.fromHtml("X<sup>2</sup>", Html.FROM_HTML_MODE_LEGACY));
また、24より前の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>"));
}
この回答は、 https://stackoverflow.com/a/37905107/4998704から導き出されました。
フラグとその他のドキュメントはここにあります: https ://developer.android.com/reference/android/text/Html.html
文字のAndroid文字列リソースの上付き文字と下付き文字
必要な文字のいずれかがここに表示されている場合は、実際にはhtmlドキュメントを使用する必要はありません。
「a」の場合は、この「ᵃ」をコピーして貼り付けます。
これらの上付き文字と下付き文字のいずれかをコピーして、Android文字列リソースに直接貼り付けることができます。
例:
<string name="word_with_superscript" translatable="false">Trademark ᵀᴹ</string>
結果:商標ᵀᴹ</ p>
上付き文字と下付き文字
上付き文字の大文字 ᴬᴮᴰᴱᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾᴿᵀᵁⱽᵂ</p>
上付き文字の極小ᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖʳˢᵗᵘᵛʷˣʸỶ
下付き文字ₐₑₕᵢⱼₖₗₘₙₒₚᵣₛₜᵤᵥₓ </p>
文字列リソースファイルでまたはを使用する方法についてこの記事を見つけました:それぞれ上付き文字と下付き文字。Spannable
<sup>
<sub>
ここでのGerardoの回答に基づいて、Intでこの拡張機能を作成しました
fun Int.toSuperScript(): String {
return when (this) {
0 -> "\u2070"
1 -> "\u00B9"
2 -> "\u00B2"
3 -> "\u00B3"
4 -> "\u2074"
5 -> "\u2075"
6 -> "\u2076"
7 -> "\u2077"
8 -> "\u2078"
9 -> "\u2079"
else -> ""
}
}
ファイルでは、HTMLタグstrings.xml
を使用するだけです。<sup>3</sup>
私のために完璧に働く
例
<string name="turnoverRate">Turnover rate m<sup>3</sup>/m<sup>2</sup>/hour:</string>
それらはUnicode文字と呼ばれ、AndroidTextView
はそれらをサポートしています。このWikiから必要なスーパー/サブスクリプトをコピーします:https://en.wikipedia.org/wiki/List_of_Unicode_characters#Superscripts_and_Subscripts
yourTextView.setText(Html.fromHtml("X<sup>2</sup>"));
This will be the result in you yourTextView =
X 2