我发现在我使用StaticLayout
的自定义视图中,如果我使用TextPaint
的子类而不是TextPaint
本身,则连字符会中断。
我的自定义视图:
public class MyView extends View {
String mText = "Using TextPaint: Recommendations";
TextPaint mTextPaint;
StaticLayout mStaticLayout;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mTextPaint = new TextPaint2(); // Works fine with TextPaint();
mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
int width = Math.round(100 * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
StaticLayout.Builder builder = StaticLayout.Builder.obtain(mText, 0, mText.length(), mTextPaint, width)
.setHyphenationFrequency(Layout.HYPHENATION_FREQUENCY_FULL);
mStaticLayout = builder.build();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(getPaddingLeft(), getPaddingTop());
mStaticLayout.draw(canvas);
canvas.restore();
}
}
我的TextPaint
的子类:
public class TextPaint2 extends TextPaint {
// Doesn't override anything
}
有谁知道这里发生了什么?这是一个错误,如果是这样,是否有解决方法?