Android去除TextView文本中的默认内边距
class NoPaddingTextView1 @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
): AppCompatTextView(context,attrs,defStyleAttr){
private val rect = Rect()
override fun onMeasure(widthMeasureSpec: Int,heightMeasureSpec: Int){
super.onMeasure(widthMeasureSpec,heightMeasureSpec)
calculateTextParams()
setMeasuredDimension(rect.right - rect.left,-rect.top + rect.bottom)
}
override fun onSizeChanged(w: Int,h: Int,oldw: Int,oldh: Int){
super.onSizeChanged(w,h,oldw,oldh)
}
override fun onDraw(canvas: Canvas?){
drawText(canvas)
}
private fun calculateTextParams(): String {
var text =text.toString()
if(TextUtils.isEmpty(text)){
text = ""
}
val textLength = text.length
paint.getTextBounds(text,0,textLength,rect)
if(textLength == 0){
rect.right = rect.left
}
return text
}
private fun drawText(canvas: Canvas?){
val text = calculateTextParams()
val left = rect.left
val bottom = rect.bottom
rect.offset(-rect.left,-rect.top)
paint.isAntiAlias = true
paint.color =currentTextColor
canvas?.drawText(text,(-left).toFloat(),(rect.bottom - bottom).toFloat(),paint)
【Android去除TextView文本中的默认内边距】}
推荐阅读
- android第三方框架(五)ButterKnife
- Android中的AES加密-下
- 带有Hilt的Android上的依赖注入
- android|android studio中ndk的使用
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库
- 深入理解|深入理解 Android 9.0 Crash 机制(二)
- android防止连续点击的简单实现(kotlin)
- Android|Android install 多个设备时指定设备