2 回答

TA貢獻1811條經驗 獲得超5個贊
在上面的代碼中,您必須在活動類中設置文本,或者在布局文件夾中.xml文件。
textView.setText( "21st, 22nd, 11th, 13th");
或 xml 文件文本視圖
android:text= "21st, 22nd, 11th, 13th"
像這樣,那里沒有自動放置上標。

TA貢獻1804條經驗 獲得超7個贊
我已經編寫了自己的邏輯來添加帶有數字的上標。在這里,我正在使用kotlin在RecyclerView適配器上工作,我正在共享onBindViewHolder的代碼。
var stringBuilder: SpannableStringBuilder = SpannableStringBuilder()
override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
// for 1 till 9
if (yourNo in 1..9) {
if (yourNo == 1)
changeSuperScripts(1, 3, yourNo + "st", holder.textView)
else if (contestStandingItem.rank == 2)
changeSuperScripts(1, 3, yourNo + "nd", holder.textView)
else if (contestStandingItem.rank == 3) {
changeSuperScripts(1, 3, yourNo + "rd", holder.textView)
} else
changeSuperScripts(1, 3, yourNo + "th", holder.textView)
}
// for 10 till 19
else if (yourNo in 10..19)
changeSuperScripts(2, 4, yourNo + "th", holder.textView)
// for 20 till 99
else if(yourNo in 20..99) {
if (yourNo % 10 == 0)
changeSuperScripts(2, 4, yourNo + "th", holder.textView)
else if (yourNo % 10 == 1)
changeSuperScripts(2, 4, yourNo + "st", holder.textView)
else if (yourNo % 10 == 2)
changeSuperScripts(2, 4, yourNo + "nd", holder.textView)
else if (yourNo % 10 == 3)
changeSuperScripts(2, 4, yourNo + "rd", holder.textView)
else
changeSuperScripts(2, 4, yourNo + "th", holder.textView)
}
// for 100 till 999
else if (yourNo in 100..999){
if (yourNo % 10 == 0)
changeSuperScripts(3, 5, yourNo + "th", holder.textView)
else if (yourNo % 10 == 1)
changeSuperScripts(3, 5, yourNo + "st", holder.textView)
else if (yourNo % 10 == 2)
changeSuperScripts(3, 5, yourNo + "nd", holder.textView)
else if (yourNo % 10 == 3)
changeSuperScripts(3, 5, yourNo + "rd", holder.textView)
else
changeSuperScripts(3, 5, yourNo + "th", holder.textView)
}
}
開始和結束是超級裂縫開始和停止的索引
fun changeSuperScripts(start: Int, end: Int, text: String, textView: TextView) {
stringBuilder.clear()
stringBuilder.append(text)
val formatter = OrdinalSuperscriptFormatter.getInstance(stringBuilder)
formatter.createSuperscriptSpan(start, end, textView)
}
這是一個用java編寫的單獨類,用于將文本轉換為上標,我已經將其設置為每次使用相同的對象的單例(僅用于性能)
公共類 OrdinalSuperscriptFormatter {
public static OrdinalSuperscriptFormatter ordinalSuperscriptFormatter;
private static final String SUPERSCRIPT_REGEX = "(?<=\\b\\d{0,10})(st|nd|rd|th)(?=\\b)";
private static final Pattern PATTERN = Pattern.compile(SUPERSCRIPT_REGEX);
private static final float PROPORTION = 0.5f;
SuperscriptSpan superscript = new SuperscriptSpan();
RelativeSizeSpan size = new RelativeSizeSpan(PROPORTION);
private final SpannableStringBuilder stringBuilder;
public static OrdinalSuperscriptFormatter getInstance(SpannableStringBuilder stringBuilder){
if (ordinalSuperscriptFormatter == null) {
ordinalSuperscriptFormatter = new OrdinalSuperscriptFormatter(stringBuilder);
return ordinalSuperscriptFormatter;
}
else
return ordinalSuperscriptFormatter;
}
public static void refreshInstance(){
ordinalSuperscriptFormatter = null;
}
private OrdinalSuperscriptFormatter(@NonNull SpannableStringBuilder stringBuilder) {
this.stringBuilder = stringBuilder;
}
public void createSuperscriptSpan(int start, int end,TextView textView) {
stringBuilder.setSpan(superscript, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
stringBuilder.setSpan(size, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(stringBuilder);
}
}
refreshInstance 將在您開始活動時調用,或者再次斷開以刷新列表
override fun onResume() {
super.onResume()
OrdinalSuperscriptFormatter.refreshInstance() // must be referesh singleton instance for each history standings
}
添加回答
舉報