gpt4 book ai didi

android - TextView 上的 requestLayout() 不更新跨度

转载 作者:行者123 更新时间:2023-12-05 00:05:39 31 4
gpt4 key购买 nike

根据官方documentation ,如果您需要在不使用繁重的 setText() 例程的情况下更新之前在 TextView 中添加的跨度,您可以保留对需要更新的跨度的引用:

Change internal span attributes

If you need to change only an internal attribute of a mutable span, such as the bullet color in a custom bullet span, you can avoid the overhead from calling setText() multiple times by keeping a reference to the span as it's created. When you need to modify the span, you can modify the reference and then call either invalidate() or requestLayout() on the TextView, depending on the type of attribute that you changed.

In the code example below, a custom bullet point implementation has a default color of red that changes to gray when clicking a button:

public class MainActivity extends AppCompatActivity {

private BulletPointSpan bulletSpan = new BulletPointSpan(Color.RED);

@Override
protected void onCreate(Bundle savedInstanceState) {
...
SpannableString spannable = new SpannableString("Text is spantastic");
// setting the span to the bulletSpan field
spannable.setSpan(bulletSpan, 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
styledText.setText(spannable);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// change the color of our mutable span
bulletSpan.setColor(Color.GRAY);
// color won’t be changed until invalidate is called
styledText.invalidate();
}
});
}
}

因此,为了复制它,我将一个 DrawableSpan 附加到我传递到我的 TextView 的可跨越文本,然后在我的可绘制对象准备就绪时更新该跨度(从远处获取)。问题是我对 requestLayout() 的调用没有更新我刚刚更新的跨度

 DrawableSpan span = new DrawableSpan();
text.setSpan(span, i1, i2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(text, TextView.BufferType.SPANNABLE);
mRequestManager.load(source).into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
span.setDrawable(resource);
textView.requestLayout();
}
});

这是我使用的DrawableSpan,它是框架的精确复制粘贴DynamicDrawableSpan这是 ImageSpan 的父类(super class)(它只是为从各种输入加载 drawalbe 提供重载)。我必须创建自己的跨度,以便稍后设置可绘制对象。

public class DrawableSpan extends ReplacementSpan {

private static final String TAG = "DynamicDrawableSpan";

/**
* A constant indicating that the bottom of this span should be aligned
* with the bottom of the surrounding text, i.e., at the same level as the
* lowest descender in the text.
*/
public static final int ALIGN_BOTTOM = 0;

/**
* A constant indicating that the bottom of this span should be aligned
* with the baseline of the surrounding text.
*/
public static final int ALIGN_BASELINE = 1;

private final int mVerticalAlignment;
private Drawable mDrawable;

public DrawableSpan() {
this(null);
}

public DrawableSpan(Drawable drawable) {
this(drawable, ALIGN_BOTTOM);
}

public DrawableSpan(Drawable drawable, int verticalAlignment) {
setDrawable(drawable);
mVerticalAlignment = verticalAlignment;
}

public void setDrawable(Drawable drawable) {
if (drawable == null) return;
mDrawable = drawable;
mDrawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
}

@Override
public int getSize(Paint paint, CharSequence text, int start, int end,
Paint.FontMetricsInt fm) {
if (mDrawable == null) return 0;
Rect rect = mDrawable.getBounds();
if (fm != null) {
fm.ascent = -rect.bottom;
fm.descent = 0;
fm.top = fm.ascent;
fm.bottom = 0;
}
return rect.right;
}

@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x,
int top, int y, int bottom, Paint paint) {

if (mDrawable == null) return;

canvas.save();
int transY = bottom - mDrawable.getBounds().bottom;
if (mVerticalAlignment == ALIGN_BASELINE) {
transY -= paint.getFontMetricsInt().descent;
}
canvas.translate(x, transY);
mDrawable.draw(canvas);
canvas.restore();
}

}

最奇怪的是,如果我锁定和解锁设备,将调用 getSize() 和 draw() 方法并显示跨度(不应用上升和顶部测量,但至少显示)。

最佳答案

我认为这是一个错误,我提交了一些非常相似的 https://issuetracker.google.com/issues/189734456

关于android - TextView 上的 requestLayout() 不更新跨度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58346404/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com