当前位置:网站首页>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"/>
边栏推荐
- [figure database performance and scenario test sharp tool ldbc SNB] series I: introduction to data generator & Application to ges service
- 中低速航空航天电子总线概述
- Im instant messaging development application keeping alive process anti kill
- How can I persuade leaders to use DDD to construct the liver project?
- MySQL semi sync replication
- D manual destruction may violate memory security
- 节奏快?压力大?VR全景客栈带你体验安逸生活
- Power application of 5g DTU wireless communication module
- 离散数学及其应用 2018-2019学年春夏学期期末考试 习题详解
- Investment analysis and prospect forecast report of global and Chinese triglycine sulfate industry from 2022 to 2028
猜你喜欢
部门新来的00后真是卷王,工作没两年,跳槽到我们公司起薪18K都快接近我了
MySQL log management
时间统一系统
The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply
Dynamic effect of canvas lines
Ott marketing is booming. How should businesses invest?
ArcGIS加载免费在线历史影像作为底图(不需要插件)
A small program written this week
U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency
Alternative to log4j
随机推荐
Why are life science enterprises on the cloud in succession?
Wx applet jump page
What is test development? Can you find a job at this stage?
Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
Requests Library
Adding, deleting, modifying and checking in low build code
D omit parameter name
What is the difference between one way and two way ANOVA analysis, and how to use SPSS or prism for statistical analysis
技术分享| WVP+ZLMediaKit实现摄像头GB28181推流播放
【Proteus仿真】定时器0作为16位计数器使用示例
同济、阿里获CVPR最佳学生论文,李飞飞获黄煦涛奖,近6000人线下参会
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
Power application of 5g DTU wireless communication module
[interview question] the difference between instancof and getclass()
节奏快?压力大?VR全景客栈带你体验安逸生活
Svg+js keyboard control path
JPA learning 2 - core annotation, annotation addition, deletion, modification and query, list query result return type, one to many, many to one, many to many
How to use promise Race() and promise any() ?
Intensive reading of thinking about markdown
有趣的checkbox计数器