国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 自定義TextView去除paddingTop和paddingBottom

2019-10-22 18:28:25
字體:
來源:轉載
供稿:網友

Android 自定義TextView去除paddingTop和paddingBottom

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

Android,自定義TextView,自定義TextView去除paddingTop

網上有很多解決方案,例如在xml中設置如下屬性:

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

或者設置margin為負值等等。 但是以上方法在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;  }}

如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 璧山县| 广南县| 东乡| 漳浦县| 邢台县| 六枝特区| 炉霍县| 来安县| 三江| 孟村| 南和县| 黄浦区| 顺平县| 宁国市| 连平县| 易门县| 加查县| 嘉祥县| 赣榆县| 平泉县| 永福县| 普陀区| 息烽县| 东明县| 财经| 佳木斯市| 镇康县| 盖州市| 南陵县| 顺昌县| 罗定市| 汉寿县| 雷州市| 石门县| 四子王旗| 昌乐县| 太谷县| 班戈县| 丹凤县| 化隆| 石林|