我有一个使用本地化的应用程序,在某些语言中,它对于它所拥有的空间来说太大了,所以我正在做的是缩放它以适应,这是我到目前为止的代码:
if(Build.VERSION.SDK_INT >= 11.0){
TypedValue tv = new TypedValue();
getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true);
actionBarWidth = getResources().getDimensionPixelSize(tv.resourceId);
}
View infoButton = findViewById(R.id.info_button);
int infoWidth = infoButton.getMeasuredWidth();
DisplayMetrics metrics = new DisplayMetrics();
Display Screen = getWindowManager().getDefaultDisplay();
Screen.getMetrics(metrics);
int screenWidth = metrics.widthPixels;
int titleWidth = screenWidth - infoWidth - actionBarWidth;
TextView titleText = (TextView) findViewById(R.id.title_text);
titleText.setText("hello hello hello hello hello hello");
TextPaint paint = titleText.getPaint();
Rect rect = new Rect();
String text = String.valueOf(titleText.getText());
int textLength = text.length();
paint.getTextBounds(text, 0, textLength,rect);
if(rect.width() > titleWidth){
float size = titleText.getTextSize();
float factor = activityHelper.getScreenMetrics();
float product = size*factor;
titleText.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);
size = titleText.getTextSize();
}
所以这使得文本变得很大,但我想要它做的是在它所拥有的房间内完美地缩放文本,我如何根据它所拥有的房间设置文本大小?
编辑:
我可以添加一个 while 循环,每次循环时都会使文本变小,检查它是否适合所需的空间吗?
最佳答案
Android 本身不支持此功能(遗憾的是)。
请参阅该答案以获取更多帮助:Auto Scale TextView Text to Fit within Bounds
关于java - Android 缩放文本以适合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23221005/