当前位置:网站首页>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"/>
边栏推荐
- 【排行榜】Carla leaderboard 排行榜 运行与参与手把手教学
- MySQL日志管理
- C程序设计专题 18-19年期末考试习题解答(下)
- 微搭低代码中实现增删改查
- Hibernate learning 3 - custom SQL
- C program design topic 18-19 final exam exercise solutions (Part 2)
- 软件测试与游戏测试文章合集录
- Go crawler framework -colly actual combat (II) -- Douban top250 crawling
- Power application of 5g DTU wireless communication module
- What is the difference between one way and two way ANOVA analysis, and how to use SPSS or prism for statistical analysis
猜你喜欢

在滴滴和字节跳动干了 5年软件测试,太真实…

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

After 5 years of software testing in didi and ByteDance, it's too real

Go crawler framework -colly actual combat (III) -- panoramic cartoon picture capture and download

Why do more and more physical stores use VR panorama? What are the advantages?

传输层 以字节为单位的滑动窗口技术

Dynamic effect of canvas lines

Tutorial details | how to edit and set the navigation function in the coolman system?

VR全景怎么赚钱?结合市场从两个方面客观分析下

Discrete mathematics and its application detailed explanation of exercises in the final exam of spring and summer semester of 2018-2019 academic year
随机推荐
Intensive reading of thinking about markdown
Human body transformation vs digital Avatar
How does VR panorama make money? Based on the objective analysis of the market from two aspects
Tongji and Ali won the CVPR best student thesis, lifeifei won the Huang xutao award, and nearly 6000 people attended the offline conference
vim使用命令
C WinForm maximizes occlusion of the taskbar and full screen display
Canvas spiral style animation JS special effect
Hyperledger Fabric 2. X dynamic update smart contract
svg+js键盘控制路径
Transition from digitalization to intelligent manufacturing
December 6, 2019 what happens after the browser enters a URL
Fuxin Kunpeng joins in, and dragon lizard community welcomes a new partner in format document technical service
Eye gaze estimation using webcam
5G dtu无线通信模块的电力应用
JPA learning 1 - overview, JPA, JPA core annotations, JPA core objects
软件测试与游戏测试文章合集录
Zed acquisition
One way 和two way ANOVA分析的区别是啥,以及如何使用SPSS或者prism进行统计分析
Im instant messaging development application keeping alive process anti kill
UE4 WebBrowser图表不能显示问题