TextView 去除默认内边距
生活随笔
收集整理的这篇文章主要介绍了
TextView 去除默认内边距
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
背景:产品姐姐需要吧¥符号和123456785价格等text内容的内边距去掉。
NumberTextView代码: package com.devleo.text.demo;import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet;import androidx.annotation.Nullable; import androidx.appcompat.widget.AppCompatTextView;/*** @author DevleoDeng*/ public class NumberTextView extends AppCompatTextView {private Rect mRect;public NumberTextView(Context context) {super(context);}public NumberTextView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);}public NumberTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);if (mRect == null) {//该坐标是以(0,0)为基准的矩形坐标mRect = new Rect();}getPaint().getTextBounds(getText().toString(), 0, getText().length(), mRect);final int width = mRect.width();final int height = mRect.height();setMeasuredDimension(width, height);}@Overrideprotected void onDraw(Canvas canvas) {final String text = getText().toString();final int left = mRect.left;final int top = mRect.top;Paint paint = getPaint();paint.setColor(getCurrentTextColor());canvas.drawText(text, -left, -top, paint);} }
总结
以上是生活随笔为你收集整理的TextView 去除默认内边距的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 想进阿里必须啃透的 13 道 MySQL
- 下一篇: 空间变换方法