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
- 下一篇: 空间变换方法