我试图在自动定径TextViews中使用RecyclerView,但是当我滚动几次时,文本变得非常小,显然不能正常工作。
我的TextView示例:
<android.support.v7.widget.AppCompatTextView
android:id="@+id/textview_unit_title"
android:layout_width="@dimen/tile_image_size"
android:layout_height="wrap_content"
android:maxLines="2"
android:textSize="@dimen/medium_size"
android:textColor="@color/color_text"
android:paddingTop="@dimen/padding_title"
android:layout_marginRight="2dp"
android:layout_marginEnd="2dp"
app:autoSizeMaxTextSize="@dimen/style_medium"
app:autoSizeTextType="uniform"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toRightOf="@id/imageview_unit_icon"
app:layout_constraintTop_toTopOf="parent"/>
我应该在其他地方以编程方式更新此缩放,还是有其他解决方案?
发布于 2018-06-05 09:41:25
Android8.0(APILevel26)允许您指示TextView根据TextView的特性和边界,让文本大小自动展开或收缩,以填充其布局。
注意:如果在XML文件中设置自动调整大小,则不建议将"wrap_content“值用于TextView的layout_width或layout_height属性。它可能会产生意想不到的结果。
你应该把高度绑起来
android:layout_height="30dp"
发布于 2018-10-12 22:13:56
我看到的问题是,将视图高度设置为wrap_content
允许文本大小变小,但是文本将永远不会变大。这就是为什么文档建议不要对视图大小使用wrap_content
。但是,我发现如果您关闭自动调整大小,将文本大小设置为最大大小,然后重新启用自动调整大小,文本大小重置为最大大小,并在必要时缩小。
因此,我的XML视图如下所示:
<android.support.v7.widget.AppCompatTextView
android:id="@+id/text_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textSize="42sp"
app:autoSizeMinTextSize="26dp"
app:autoSizeMaxTextSize="42dp"
app:autoSizeTextType="none"/>
然后在我的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);
}
});
发布于 2019-05-16 09:39:36
帕维尔·哈鲁扎的回答非常好。然而,它没有起作用,可能是因为他错过了一条线setTextSize(TypedValue.COMPLEX_UNIT_PX, maxTextSize);
。
以下是我的最新版本:
public class MyTextView extends AppCompatTextView {
private int minTextSize;
private int maxTextSize;
private int granularity;
public MyTextView(Context context) {
super(context);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
minTextSize = TextViewCompat.getAutoSizeMinTextSize(this);
maxTextSize = TextViewCompat.getAutoSizeMaxTextSize(this);
granularity = Math.max(1, TextViewCompat.getAutoSizeStepGranularity(this));
}
@Override
public void setText(CharSequence text, BufferType type) {
// this method is called on every setText
disableAutoSizing();
setTextSize(TypedValue.COMPLEX_UNIT_PX, maxTextSize);
super.setText(text, type);
post(this::enableAutoSizing); // enable after the view is laid out and measured at max text size
}
private void disableAutoSizing() {
TextViewCompat.setAutoSizeTextTypeWithDefaults(this, TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);
}
private void enableAutoSizing() {
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(this,
minTextSize, maxTextSize, granularity, TypedValue.COMPLEX_UNIT_PX);
}}
https://stackoverflow.com/questions/50696258
复制相似问题