我想使用许多textViews进行分割的Activity,其大小将填充可用空间。我正在尝试在Java和Android Studio中处理它。
我试图在布局中设置它们的autoTextSize属性,但是对于各种长度的单词,它们的大小是不同的-尽管空间相同。我试图使用下面编写的函数来标准化字体大小。
private void standardizeTheFontSize() {
TextView[] textViewsArray =
{
findViewById(R.id.menu_module1), findViewById(R.id.menu_module2),
findViewById(R.id.menu_module3), findViewById(R.id.menu_module4),
findViewById(R.id.menu_module5), findViewById(R.id.menu_module6),
findViewById(R.id.menu_module7), findViewById(R.id.menu_module7_1)
};
float textSize = ((TextView)findViewById(R.id.textView_menu)).getTextSize();
findViewById(R.id.textView_menu).measure(0,0);
int height = findViewById(R.id.textView_menu).getMeasuredHeight();
for (TextView textView : textViewsArray) {
textSize = (textView.getTextSize() < textSize) ? textView.getTextSize() : textSize;
textView.measure(0,0);
height = (textView.getMeasuredHeight() < height)?textView.getMeasuredHeight():height;
}
for (TextView textView : textViewsArray) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
textView.setHeight(height);
}
}
我的想法好吗,但条件有些糟糕,或者我应该尝试其他方法-也许有更简单的解决方案:)