作者:吉之麟 | 来源:互联网 | 2022-12-15 04:34
我正在尝试在RecyclerView中使用Autosizing TextViews,但是当我滚动几次时,文本变得很小,以至于显然不能正常工作。
我的TextView的示例:
我应该以编程方式在其他地方更新此缩放比例,还是有其他解决方案?
1> Michael Cele..:
我所看到的问题是,将视图高度设置为可以wrap_content
使文本大小变小,但是文本再也不会变大。这就是为什么文档建议不要wrap_content
用于视图大小的原因。但是,我发现如果关闭自动调整大小,将文本大小设置为最大值,然后重新启用自动调整大小,则文本大小将重置为最大大小,并在必要时缩小。
因此,我在XML中的视图如下所示:
然后在我ViewHolder
将文本绑定到视图时:
TextView title = view.findViewById(R.id.text_title);
String titleValue = "Some Title Value";
// Turn off auto-sizing text.
TextViewCompat.setAutoSizeTextTypeWithDefaults(title,
TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);
// Bump text size back up to the max value.
title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 42);
// Set your text as normal.
title.setText(titleValue);
// Post a runnable to re-enable auto-sizing text so that it occurs
// after the view is laid out and measured at max text size.
title.post(new Runnable() {
@Override
public void run() {
TextViewCompat
.setAutoSizeTextTypeUniformWithConfiguration(title,
26, 42, 1, TypedValue.COMPLEX_UNIT_DIP);
}
});
2> IntelliJ Ami..:
Autosizing TextViews
Android 8.0(API级别26)允许您指示TextView根据TextView的特征和边界自动扩大或缩小文本大小以填充其布局。
注意:如果在XML文件中设置自动调整大小,则不建议对TextView的layout_width或layout_height属性使用值“ wrap_content”。可能会产生意想不到的结果。
你应该约束高度
android:layout_
好吧,这是一个限制。