当我包括下面XML
来布局文件,我可以看到下面的图片.如果你看到它,你会发现它TextView
有顶部和底部空间.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E1"
android:background="#ff00ff00"/>
Run Code Online (Sandbox Code Playgroud)
我希望删除这个空间.如何删除它?这叫什么?如果有人有线索...请告诉我.提前致谢.
Gra*_*and 88
试着android:includeFontPadding="false"
看看它是否有帮助.根据我的经验,这将有所帮助,但没有办法将TextView尺寸缩小到精确的像素完美文本大小.
唯一可能或可能不会产生更好结果的替代方案是作弊并硬化尺寸以匹配文本大小,例如"24sp"
而不是"wrap_content"
高度.
Ant*_*ton 22
我有同样的问题.属性android:includeFontPadding="false"
对我不起作用.我用这种方式解决了这个问题:
public class TextViewWithoutPaddings extends TextView {
private final Paint mPaint = new Paint();
private final Rect mBounds = new Rect();
public TextViewWithoutPaddings(Context context) {
super(context);
}
public TextViewWithoutPaddings(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewWithoutPaddings(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(@NonNull Canvas canvas) {
final String text = calculateTextParams();
final int left = mBounds.left;
final int bottom = mBounds.bottom;
mBounds.offset(-mBounds.left, -mBounds.top);
mPaint.setAntiAlias(true);
mPaint.setColor(getCurrentTextColor());
canvas.drawText(text, -left, mBounds.bottom - bottom, mPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
calculateTextParams();
setMeasuredDimension(mBounds.width() + 1, -mBounds.top + 1);
}
private String calculateTextParams() {
final String text = getText().toString();
final int textLength = text.length();
mPaint.setTextSize(getTextSize());
mPaint.getTextBounds(text, 0, textLength, mBounds);
if (textLength == 0) {
mBounds.right = mBounds.left;
}
return text;
}
}
Run Code Online (Sandbox Code Playgroud)
j2e*_*nue 20
尝试将您的底部和顶部边距设置为负数.
这样的事情:
android:layout_marginTop="-5dp"
android:layout_marginBottom="-5dp"
Run Code Online (Sandbox Code Playgroud)
相应地调整值.
这是拯救我们的代码。它是使用maksimko的 mono C# 代码进行改编的:
public class TopAlignedTextView extends TextView {
public TopAlignedTextView(Context context) {
super(context);
}
/*This is where the magic happens*/
@Override
protected void onDraw(Canvas canvas){
float offset = getTextSize() - getLineHeight();
canvas.translate(0, offset);
super.onDraw(canvas);
}
}
Run Code Online (Sandbox Code Playgroud)
仍然需要尝试一下,textView.setIncludeFontPadding(false)
因为我们要使用TextViews
不同的字体大小。
您定义了布局边距吗?例如:
android:layout_marginTop="5dp"
Run Code Online (Sandbox Code Playgroud)
否则,如果您的文本视图包含在 LinearLayout 或其他容器内,则该冷视图也具有填充或边距。
归档时间: |
|
查看次数: |
43455 次 |
最近记录: |