Android如何自定義TextView去除paddingTop和paddingBottom

這篇文章給大家分享的是有關(guān)Android如何自定義TextView去除paddingTop和paddingBottom的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)建站主營青神網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,app軟件定制開發(fā),青神h5微信小程序開發(fā)搭建,青神網(wǎng)站營銷推廣歡迎青神等地區(qū)企業(yè)咨詢

Android 自定義TextView去除paddingTop和paddingBottom

最近項(xiàng)目中需要用libgdx渲染一個(gè)Android的TextView, 但是繪制出來的TextView總是默認(rèn)帶有paddingTop和paddingBottom, 如下圖所示:

Android如何自定義TextView去除paddingTop和paddingBottom

網(wǎng)上有很多解決方案,例如在xml中設(shè)置如下屬性:

android:lineSpacingMultiplier="0.8"
android:includeFontPadding="false"

或者設(shè)置margin為負(fù)值等等。 但是以上方法在6.0之后都沒什么卵用。

只有一種方法可以做到,就是自定義TextView

package com.ef.smallstar.common.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.util.AttributeSet;

/**
 * Created by Danny on 17/8/28.
 *
 * this is a Android TextView without padding top & padding bottom
 */

public class TextViewWithoutPadding extends android.support.v7.widget.AppCompatTextView {

  private final Paint mPaint = new Paint();

  private final Rect mBounds = new Rect();

  public TextViewWithoutPadding(Context context) {
    super(context);
  }

  public TextViewWithoutPadding(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public TextViewWithoutPadding(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;
  }
}

感謝各位的閱讀!關(guān)于“Android如何自定義TextView去除paddingTop和paddingBottom”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

分享名稱:Android如何自定義TextView去除paddingTop和paddingBottom
分享鏈接:http://muchs.cn/article32/ipposc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、Google、靜態(tài)網(wǎng)站、網(wǎng)站設(shè)計(jì)品牌網(wǎng)站設(shè)計(jì)、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)