import android.annotation.SuppressLint import android.content.Context import android.graphics.Canvas import android.util.AttributeSet import android.widget.TextView /** * Author: yangtianfu * Date: 2021/1/25 18:08 * Describe:drawableLeft is shown in center */ @suppressLint ("AppCompatCustomView") class DrawableCenterTextView: TextView { constructor( context: Context? , attrs: AttributeSet? , defStyle: Int ) : super(context, attrs, defStyle){} constructor(context: Context? , attrs: AttributeSet?) : super(context, attrs) {} constructor(context: Context?) : super(context) {} override fun onDraw(canvas: Canvas) { val drawables = compoundDrawables if (drawables ! = null) { val drawableLeft = drawables[0] if (drawableLeft ! = null) { val textWidth = paint.measureText(text.toString()) val drawablePadding = compoundDrawablePadding var drawableWidth = drawableLeft.intrinsicWidth val bodyWidth = textWidth + drawableWidth + drawablePadding canvas.translate((width - bodyWidth) / 2, 0F) } } super.onDraw(canvas) } }Copy the code

use

<DrawableCenterTextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:text="联系"
                android:drawablePadding="8dp"
                android:drawableLeft="@drawable/icon_call"
                android:textColor="#ff000000"
                android:textSize="14sp"/>
Copy the code