如何真正删除 (AutoResize)TextView 中的 Padding?
How to really remove the Padding in an (AutoResize)TextView?
上下文:
我想要一个 TextView,它会自动将其文本大小调整为屏幕宽度。因此我下载了AutoResizeTextView based on the post by Chase。
它工作得很好,有时视图在设备上仍在增长或缩小,但我可以接受。但是,我真的很想对 TextView 进行非常有限的填充,以充分利用屏幕上剩余的 space。因此我扩展了 class 如下:
public class AutoResizeTextViewNoPadding extends AutoResizeTextView {
public AutoResizeTextViewNoPadding(Context context) {
super(context);
}
public AutoResizeTextViewNoPadding(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoResizeTextViewNoPadding(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
int yOffset = getHeight() - getBaseline() - (int)super.getTextSize()/15;
// this does not work, gives a blank view
// int bottom = getHeight() - getBaseline() - (int)super.getTextSize()/15;
// int top = getHeight() - bottom; // absolute number of space cut on bottom, equals top
// canvas.clipRect(0,top,getWidth(),bottom);
// canvas.clipRect(0, top, getWidth(), bottom, Region.Op.REPLACE);
// super.onDraw(canvas);
// this does not work, also gives a blank view
// Bitmap bitmap= Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight() - 2*yOffset, Bitmap.Config.ARGB_8888);
// Paint p = getPaint();
// Canvas othercanvas = new Canvas();
// othercanvas.drawBitmap(bitmap,0,0,p);
// super.onDraw(othercanvas);
// this works to remove the FontPadding on the bottom, but then the top gets more Padding of course
canvas.translate(0, yOffset);
super.onDraw(canvas);
}
}
问题:
canvas.translate 将实际文本移向视图底部 (yOffset)。但我实际上希望从视图的底部和顶部裁剪这个量 (yOffset)。正如在代码中看到的那样,我尝试了两种都不起作用的方法,即我看到一个空视图,其大小与正常 (AutoResize)TextView 时的大小相同。这能做到吗?
或者这个问题可以通过其他方式解决吗?请注意,设置负边距将不起作用,因为文本大小范围很大,因此边距也必须在范围内。或者我可以在 (AutoResize)TextView(NoPadding) class 中的某处设置边距吗?
它并不完美,但如果有人正在寻找相同的东西,这或多或少可以做到(对于 android:singleLine="true" 的 AutoResizableTextViews):
public class AutoResizeTextViewNoPadding extends TextView
{
(...)
@Override
public int onTestSize(final int suggestedSize,final RectF availableSPace)
{
paint.setTextSize(suggestedSize);
final String text=getText().toString();
final boolean singleline=getMaxLines()==1;
if(singleline)
{
textRect.bottom=(float)(paint.getFontSpacing()*.8);
textRect.right=paint.measureText(text);
}
(...)
}
@Override
protected void onDraw(Canvas canvas) {
int yOffset = getHeight() - getBaseline() - (int)super.getTextSize()/20;
canvas.translate(0, 2*yOffset);
super.onDraw(canvas);
}
}
上下文:
我想要一个 TextView,它会自动将其文本大小调整为屏幕宽度。因此我下载了AutoResizeTextView based on the post by Chase。 它工作得很好,有时视图在设备上仍在增长或缩小,但我可以接受。但是,我真的很想对 TextView 进行非常有限的填充,以充分利用屏幕上剩余的 space。因此我扩展了 class 如下:
public class AutoResizeTextViewNoPadding extends AutoResizeTextView {
public AutoResizeTextViewNoPadding(Context context) {
super(context);
}
public AutoResizeTextViewNoPadding(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AutoResizeTextViewNoPadding(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
int yOffset = getHeight() - getBaseline() - (int)super.getTextSize()/15;
// this does not work, gives a blank view
// int bottom = getHeight() - getBaseline() - (int)super.getTextSize()/15;
// int top = getHeight() - bottom; // absolute number of space cut on bottom, equals top
// canvas.clipRect(0,top,getWidth(),bottom);
// canvas.clipRect(0, top, getWidth(), bottom, Region.Op.REPLACE);
// super.onDraw(canvas);
// this does not work, also gives a blank view
// Bitmap bitmap= Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight() - 2*yOffset, Bitmap.Config.ARGB_8888);
// Paint p = getPaint();
// Canvas othercanvas = new Canvas();
// othercanvas.drawBitmap(bitmap,0,0,p);
// super.onDraw(othercanvas);
// this works to remove the FontPadding on the bottom, but then the top gets more Padding of course
canvas.translate(0, yOffset);
super.onDraw(canvas);
}
}
问题:
canvas.translate 将实际文本移向视图底部 (yOffset)。但我实际上希望从视图的底部和顶部裁剪这个量 (yOffset)。正如在代码中看到的那样,我尝试了两种都不起作用的方法,即我看到一个空视图,其大小与正常 (AutoResize)TextView 时的大小相同。这能做到吗?
或者这个问题可以通过其他方式解决吗?请注意,设置负边距将不起作用,因为文本大小范围很大,因此边距也必须在范围内。或者我可以在 (AutoResize)TextView(NoPadding) class 中的某处设置边距吗?
它并不完美,但如果有人正在寻找相同的东西,这或多或少可以做到(对于 android:singleLine="true" 的 AutoResizableTextViews):
public class AutoResizeTextViewNoPadding extends TextView
{
(...)
@Override
public int onTestSize(final int suggestedSize,final RectF availableSPace)
{
paint.setTextSize(suggestedSize);
final String text=getText().toString();
final boolean singleline=getMaxLines()==1;
if(singleline)
{
textRect.bottom=(float)(paint.getFontSpacing()*.8);
textRect.right=paint.measureText(text);
}
(...)
}
@Override
protected void onDraw(Canvas canvas) {
int yOffset = getHeight() - getBaseline() - (int)super.getTextSize()/20;
canvas.translate(0, 2*yOffset);
super.onDraw(canvas);
}
}