我必须在TextView的上标和下标中显示一些字符或数字。所以我使用SpannableStringBuilder但是在Nougat中它的输出是不一致的。
public class MainActivity extends AppCompatActivity {
ArrayList<SpannableStringBuilder> MyList = new ArrayList<>();
TextView TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView = (TextView)findViewById(R.id.textView);
}
void AddText(){
SpannableStringBuilder txt = new SpannableStringBuilder("TEST");
txt.setSpan(new SuperscriptSpan(), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txt.setSpan(new RelativeSizeSpan(0.5f), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
MyList.add(txt);
SpannableStringBuilder finalText = new SpannableStringBuilder(MyList.get(0));
for (int i=1; i<=MyList.size()-1; i++)
finalText.append(MyList.get(i));
TextView.setText(finalText);
}
public void button(View view){
AddText();
}
}