Android 自定義TextView去除paddingTop和paddingBottom
Android 自定義TextView去除paddingTop和paddingBottom
最近項(xiàng)目中需要用libgdx渲染一個(gè)Android的TextView, 但是繪制出來(lái)的TextView總是默認(rèn)帶有paddingTop和paddingBottom, 如下圖所示:
網(wǎng)上有很多解決方案,例如在xml中設(shè)置如下屬性:
android:lineSpacingMultiplier="0.8" android:includeFontPadding="false"
或者設(shè)置margin為負(fù)值等等。 但是以上方法在6.0之后都沒(méi)什么卵用。
只有一種方法可以做到,就是自定義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; } }
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android中TextView和ImageView實(shí)現(xiàn)傾斜效果
- Android TextView實(shí)現(xiàn)帶鏈接文字事件監(jiān)聽(tīng)的三種常用方式示例
- Android用戶輸入自動(dòng)提示控件AutoCompleteTextView使用方法
- Android TextView對(duì)齊的兩種方法
- Android TextView實(shí)現(xiàn)圖文混合編排的方法
- Android實(shí)現(xiàn)帶動(dòng)畫(huà)效果的可點(diǎn)擊展開(kāi)TextView
- Android TextView中部分文字高亮顯示
- Android自定義textview實(shí)現(xiàn)豎直滾動(dòng)跑馬燈效果
- Android中TextView顯示圓圈背景或設(shè)置圓角的方法
相關(guān)文章
Android編程實(shí)現(xiàn)列表側(cè)滑刪除的方法詳解
這篇文章主要介紹了Android編程實(shí)現(xiàn)列表側(cè)滑刪除的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android列表側(cè)滑刪除功能的原理與具體實(shí)現(xiàn)技巧,注釋中包含詳盡的說(shuō)明,需要的朋友可以參考下2018-01-01Android開(kāi)發(fā)X Y軸Board的繪制教程示例
這篇文章主要為大家介紹了Android開(kāi)發(fā)X Y軸Board的繪制教程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12React?Native之在Android上添加陰影的實(shí)現(xiàn)
這篇文章主要介紹了React?Native之在Android上添加陰影的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Android APP啟動(dòng)方式、啟動(dòng)流程及啟動(dòng)優(yōu)化分析
這篇文章主要介紹了Android APP啟動(dòng)方式、啟動(dòng)流程及啟動(dòng)優(yōu)化分析的相關(guān)資料,需要的朋友可以參考下2016-09-09Android實(shí)現(xiàn)生成二維碼并保存到相冊(cè)
這篇文章主要介紹了如何利用Android實(shí)現(xiàn)二維碼的生成,并且保存到本地相冊(cè)。文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編學(xué)習(xí)一下2022-04-04Android Flutter實(shí)現(xiàn)彈幕效果
這篇文章主要為大家詳細(xì)介紹如何利用Android FLutter實(shí)現(xiàn)彈幕效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06RxJava+Retrofit+OkHttp實(shí)現(xiàn)多文件下載之?dāng)帱c(diǎn)續(xù)傳
本篇文章主要介紹了RxJava+Retrofit+OkHttp實(shí)現(xiàn)多文件下載之?dāng)帱c(diǎn)續(xù)傳,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Flutter實(shí)現(xiàn)仿京東商品詳情底部操作欄
這篇文章主要為大家詳細(xì)介紹了Flutter如何仿京東實(shí)現(xiàn)商品詳情底部操作欄,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-06-06