当前位置:网站首页>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"/>
边栏推荐
- @mysql
- 水库大坝安全监测
- Unmanned driving: Some Thoughts on multi-sensor fusion
- 传输层 以字节为单位的滑动窗口技术
- Collection of software testing and game testing articles
- How to use promise Race() and promise any() ?
- Modstart: embrace new technologies and take the lead in supporting laravel 9.0
- Go crawler framework -colly actual combat (4) -- Zhihu answer crawl (2) -- visual word cloud
- C program design topic 18-19 final exam exercise solutions (Part 2)
- Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
猜你喜欢
The third generation of power electronics semiconductors: SiC MOSFET learning notes (V) research on driving power supply
【排行榜】Carla leaderboard 排行榜 运行与参与手把手教学
怎么把wps表格里某一列有重复项的整行删掉
5年,从“点点点”到现在的测试开发,我的成功值得每一个借鉴。
Outer screen and widescreen wasted? Harmonyos folding screen design specification teaches you to use it
canvas线条的动态效果
Fuxin Kunpeng joins in, and dragon lizard community welcomes a new partner in format document technical service
∞符号线条动画canvasjs特效
【面试题】什么是事务,什么是脏读、不可重复读、幻读,以及MySQL的几种事务隔离级别的应对方法
5G dtu无线通信模块的电力应用
随机推荐
Current situation analysis and development trend forecast report of global and Chinese acrylonitrile butadiene styrene industry from 2022 to 2028
2019 summary and 2020 outlook
离散数学及其应用 2018-2019学年春夏学期期末考试 习题详解
Microsoft won the title of "leader" in the magic quadrant of Gartner industrial Internet of things platform again!
Discrete mathematics and its application detailed explanation of exercises in the final exam of spring and summer semester of 2018-2019 academic year
在滴滴和字节跳动干了 5年软件测试,太真实…
教程详解|在酷雷曼系统中如何编辑设置导览功能?
C# Winform 最大化遮挡任务栏和全屏显示问题
UE4 WebBrowser chart cannot display problems
Hyperledger Fabric 2. X dynamic update smart contract
Common redis commands in Linux system
im即时通讯开发应用保活之进程防杀
Signal integrity (SI) power integrity (PI) learning notes (XXV) differential pair and differential impedance (V)
D manual destruction may violate memory security
vim使用命令
Zed acquisition
【Proteus仿真】定时器0作为16位计数器使用示例
Collective example
Difficult and miscellaneous problems: A Study on the phenomenon of text fuzziness caused by transform
U.S. House of Representatives: digital dollar will support the U.S. dollar as the global reserve currency