当前位置:网站首页>The drawableleft of the custom textview in kotlin is displayed in the center together with the text
The drawableleft of the custom textview in kotlin is displayed in the center together with the text
2022-06-25 00:21:00 【BY-91】
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 Center with text
*/
@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)
}
}
Use
<DrawableCenterTextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical"
android:text=" contact "
android:drawablePadding="8dp"
android:drawableLeft="@drawable/icon_call"
android:textColor="#ff000000"
android:textSize="14sp"/>
边栏推荐
- Requests Library
- D manual destruction may violate memory security
- Go crawler framework -colly actual combat (I)
- Eye gaze estimation using webcam
- 为什么越来越多的实体商铺用VR全景?优势有哪些?
- Fast pace? high pressure? VR panoramic Inn brings you a comfortable life
- canvas螺旋样式的动画js特效
- Do280openshift access control -- encryption and configmap
- UE4 WebBrowser图表不能显示问题
- Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
猜你喜欢
Hyperledger Fabric 2. X dynamic update smart contract

JDBC —— 数据库连接

Signal integrity (SI) power integrity (PI) learning notes (XXV) differential pair and differential impedance (V)

节奏快?压力大?VR全景客栈带你体验安逸生活

Go crawler framework -colly actual combat (II) -- Douban top250 crawling

svg+js键盘控制路径

MySQL日志管理

Tape SVG animation JS effect

JDBC - database connection

为什么越来越多的实体商铺用VR全景?优势有哪些?
随机推荐
Hyperledger Fabric 2. X dynamic update smart contract
传输层 以字节为单位的滑动窗口技术
Analysis report on operation mode and future development of global and Chinese methyl cyclopentanoate industry from 2022 to 2028
Tutorial details | how to edit and set the navigation function in the coolman system?
Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
How can I persuade leaders to use DDD to construct the liver project?
JPA learning 1 - overview, JPA, JPA core annotations, JPA core objects
[Solved] Public key for mysql-community-xxx. rpm is not installed
D manual destruction may violate memory security
MySQL日志管理
第三代电力电子半导体:SiC MOSFET学习笔记(五)驱动电源调研
WordPress add photo album function [advanced custom fields Pro custom fields plug-in series tutorial]
[interview question] what is a transaction? What are dirty reads, unrepeatable reads, phantom reads, and how to deal with several transaction isolation levels of MySQL
Transition from digitalization to intelligent manufacturing
What is test development? Can you find a job at this stage?
Hibernate learning 3 - custom SQL
Do280openshift access control -- encryption and configmap
Global and Chinese tetrahydrofurfuryl butyrate industry operation pattern and future prospect report 2022 ~ 2028
Why are life science enterprises on the cloud in succession?
怎么把wps表格里某一列有重复项的整行删掉