108

下付き文字または上付き文字を含む文字列を印刷するにはどうすればよいですか?外部ライブラリなしでこれを行うことはできますか?TextViewこれをAndroidで表示したい。

4

15 に答える 15

160
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));

また

一般的なタスクとAndroidでそれらを行う方法

于 2010-08-22T22:04:22.987 に答える
112

例:

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);
于 2012-01-15T20:13:09.793 に答える
51

上付き文字や下付き文字を作成する以外に小さくしたい場合は、タグを追加するだけです。元:

"X <sup><small> 2 </small></sup>"
于 2013-05-03T15:27:11.023 に答える
21

コードにこの「\u00B2」を入れてください。

textView.setText("X\u00B2");

于 2018-02-14T19:02:49.007 に答える
13

少し遅れましたが、うまく機能した後、特殊文字として上付き文字を使用し、ここでは空間文字を使用しました。

<string name="str">H₂&lt;/string>
于 2015-05-12T09:52:29.727 に答える
13

承認された回答は非推奨になりました。したがって、このコードを確認してください。これはいくつかのウェブサイトから入手しました。名前を忘れてしまいましたが、とにかくこの優れたコードに感謝します。

     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"てください。

于 2017-02-08T11:59:04.370 に答える
12
((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>
于 2015-04-04T09:45:48.600 に答える
11

string.xmlファイルから上付き文字を設定する場合は、次のことを試してください。

文字列リソース:

<string name="test_string">X&lt;sup&gt;3&lt;/sup&gt;</string>

上付き文字を小さくしたい場合:

<string name="test_string">X&lt;sup&gt;&lt;small&gt;3&lt;/small&gt;&lt;/sup&gt;</string>

コード:

textView.setText(Html.fromHtml("Anything you want to put here"+getString(R.string.test_string)));
于 2014-09-03T20:35:02.090 に答える
4

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

于 2016-10-17T05:21:22.173 に答える
4

文字のAndroid文字列リソースの上付き文字と下付き文字

必要な文字のいずれかがここに表示されている場合は、実際にはhtmlドキュメントを使用する必要はありません。

「a」の場合は、この「ᵃ」をコピーして貼り付けます。

これらの上付き文字と下付き文字のいずれかをコピーして、Android文字列リソースに直接貼り付けることができます。

例:

    <string name="word_with_superscript" translatable="false">Trademark ᵀᴹ&lt;/string>

結果:商標ᵀᴹ</ p>

上付き文字と下付き文字

上付き文字の大文字 ᴬᴮᴰᴱᴳᴴᴵᴶᴷᴸᴹᴺᴼᴾᴿᵀᵁⱽᵂ</p>

上付き文字の極小ᵃᵇᶜᵈᵉᶠᵍʰⁱʲᵏˡᵐⁿᵒᵖʳˢᵗᵘᵛʷˣʸỶ

下付き文字ₐₑₕᵢⱼₖₗₘₙₒₚᵣₛₜᵤᵥₓ </p>

于 2019-11-10T08:35:01.957 に答える
3

文字列リソースファイルでまたはを使用する方法についてこの記事を見つけました:それぞれ上付き文字と下付き文字。Spannable<sup><sub>

于 2010-08-22T22:01:24.810 に答える
1

ここでの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 -> ""
}

}

于 2020-11-06T05:51:18.663 に答える
0

ファイルでは、HTMLタグstrings.xmlを使用するだけです。<sup>3</sup>私のために完璧に働く

<string name="turnoverRate">Turnover rate m<sup>3</sup>/m<sup>2</sup>/hour:</string>
于 2015-07-22T07:54:46.530 に答える
0

それらはUnicode文字と呼ばれ、AndroidTextViewはそれらをサポートしています。このWikiから必要なスーパー/サブスクリプトをコピーします:https://en.wikipedia.org/wiki/List_of_Unicode_characters#Superscripts_and_Subscripts

于 2015-09-04T13:15:10.713 に答える
-1
yourTextView.setText(Html.fromHtml("X<sup>2</sup>"));

This will be the result in you yourTextView =

X 2

于 2016-02-23T04:01:14.693 に答える