Android에서 문자열 첨자 및 상위 첨자
첨자 또는 위첨자를 사용하여 문자열을 인쇄하려면 어떻게 해야 합니까?외부 라이브러리 없이 이 작업을 수행할 수 있습니까?이것을 A로 표시해 주세요.TextView
안드로이드의 경우.
((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>
((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"
스판텍트입니다.
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)));
Android 문자열 리소스 문자용 상위 문자 및 첨자
원하는 문자가 여기에 표시되어 있으면 html 문서를 사용할 필요가 없습니다.
"a"의 경우 이 """를 복사하여 붙여넣습니다.
이러한 슈퍼스크립트 및 서브스크립트를 복사하여 Android 문자열 리소스에 직접 붙여넣을 수 있습니다.
예:
<string name="word_with_superscript" translatable="false">Trademark ᵀᴹ</string>
결과 : Trademark ᵀᵀ
위첨자 및 아래첨자 문자
대문자 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자: 대문자
위첨자 미숫자: cule supers 、 supers supers 、 supers supers 、 supers supers 、 supers supers supers supers supers supers
Subscript Minculer (서브스크립트 미숫자)
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 에서 찾을 수 있습니다.
이 기사를 찾았습니다.Spannable
또는 문자열 리소스 파일:<sup>
또는<sub>
위첨자, 아래첨자.
여기 Gerardo의 답변을 바탕으로 나는 이 확장자를 만들었다.
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 -> ""
}
}
서서 strings.xml
파일, HTML 할 수 .<sup>3</sup>
하게 동작해 . 이치노
예
<string name="turnoverRate">Turnover rate m<sup>3</sup>/m<sup>2</sup>/hour:</string>
은 유니코드 문자, 안드로이드 문자, 안드로이드 문자, 유니코드 문자라고 불린다.TextView
가 서포트하고 있습니다.이 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 =
X2.
언급URL : https://stackoverflow.com/questions/3543454/subscript-and-superscript-a-string-in-android
'programing' 카테고리의 다른 글
파라미터가 다른 여러 메서드콜을 확인하는 방법 (0) | 2022.11.21 |
---|---|
조인에서 모든 열 이름 앞에 열 이름의 원본 테이블을 붙이는 방법 (0) | 2022.11.21 |
iPython의 판다 라이브러리를 사용하여 .xlsx 파일을 읽는 방법은 무엇입니까? (0) | 2022.11.21 |
PHP "pretty print" json_encode (0) | 2022.11.21 |
C 논블로킹 키보드 입력 (0) | 2022.11.02 |