TextView 多行文字,字串自適應
阿新 • • 發佈:2019-01-24
多行文字,字串自適應
- 過長的用省略號替代和字型大小控制,示例程式碼如下
tv_auto_settings_title.postDelayed(new Runnable() {
@Override
public void run() {
while (tv_auto_settings_title.getLineCount() > 2){//判斷行數大於多少時改變
int lineEndIndex = tv_auto_settings_title.getLayout().getLineEnd(1 ); //設定第2行打省略號
String text = tv_auto_settings_title.getText().subSequence(0, lineEndIndex - 3) +"...";
tv_auto_settings_title.setText(text);
tv_auto_settings_title.setTextSize(TypedValue.COMPLEX_UNIT_PX, tv_auto_settings_title.getTextSize() - 4 );
}
}
}, 50);