当前位置:网站首页>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"/>
边栏推荐
- Modstart: embrace new technologies and take the lead in supporting laravel 9.0
- C WinForm maximizes occlusion of the taskbar and full screen display
- @mysql
- How to use promise Race() and promise any() ?
- 中低速航空航天电子总线概述
- C程序设计专题 18-19年期末考试习题解答(下)
- Collection of software testing and game testing articles
- 【图数据库性能和场景测试利器LDBC SNB】系列一:数据生成器简介 & 应用于GES服务
- 美国众议院议员:数字美元将支持美元作为全球储备货币
- UE4 WebBrowser chart cannot display problems
猜你喜欢

Hibernate learning 3 - custom SQL

Adding, deleting, modifying and checking in low build code

无需显示屏的VNC Viewer远程连接树莓派

OTT营销之风正盛,商家到底该怎么投?

Fast pace? high pressure? VR panoramic Inn brings you a comfortable life

U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency

Discrete mathematics and its application detailed explanation of exercises in the final exam of spring and summer semester of 2018-2019 academic year

5G dtu无线通信模块的电力应用

wx小程序跳转页面
Hyperledger Fabric 2. X dynamic update smart contract
随机推荐
Go crawler framework -colly actual combat (I)
颜色渐变梯度颜色集合
Creative SVG ring clock JS effect
JDBC —— 数据库连接
信号完整性(SI)电源完整性(PI)学习笔记(二十五)差分对与差分阻抗(五)
Simple collation of Web cache
ArcGIS加载免费在线历史影像作为底图(不需要插件)
Collective example
2019 summary and 2020 outlook
无人驾驶: 对多传感器融合的一些思考
美国众议院议员:数字美元将支持美元作为全球储备货币
Human body transformation vs digital Avatar
人体改造 VS 数字化身
Domain Driven Design and coding
[Solved] Public key for mysql-community-xxx. rpm is not installed
Investment analysis and prospect forecast report of global and Chinese triglycine sulfate industry from 2022 to 2028
[issue 25] face to face experience of golang Engineer in the rightmost social recruitment
离散数学及其应用 2018-2019学年春夏学期期末考试 习题详解
How can I persuade leaders to use DDD to construct the liver project?
Why do more and more physical stores use VR panorama? What are the advantages?