TextView 去除默认内边距
生活随笔
收集整理的這篇文章主要介紹了
TextView 去除默认内边距
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
背景:產(chǎn)品姐姐需要吧¥符號(hào)和123456785價(jià)格等text內(nèi)容的內(nèi)邊距去掉。
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) {//該坐標(biāo)是以(0,0)為基準(zhǔn)的矩形坐標(biāo)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);} }?
總結(jié)
以上是生活随笔為你收集整理的TextView 去除默认内边距的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 想进阿里必须啃透的 13 道 MySQL
- 下一篇: 空间变换方法