当前位置:网站首页>Android修行手册之Kotlin - 自定义View的几种写法
Android修行手册之Kotlin - 自定义View的几种写法
2022-06-25 15:41:00 【芝麻粒儿】
本文约2.6千字,新手阅读需要4分钟,复习需要1分钟 【收藏随时查阅不再迷路】
关于作者
众所周知,人生是一个漫长的流程,不断克服困难,不断反思前进的过程。在这个过程中会产生很多对于人生的质疑和思考,于是我决定将自己的思考,经验和故事全部分享出来,以此寻找共鸣 !!!
专注于Android/Unity和各种游戏开发技巧,以及各种资源分享(网站、工具、素材、源码、游戏等)
有什么需要欢迎私我,交流群让学习不再孤单。

前提
这是小空坚持写的Android新手向系列,欢迎品尝。
大佬(√)
新手(√√√)
实践过程
使用Kotlin还不是很习惯,记录笔记。
有三种方式
方式一
推荐,注意前两个构造参数是this,而不是super
class WaveViewKotlin: View{
constructor(context: Context?) :this(context,null)
constructor(context: Context?, attrs: AttributeSet?) :this(context,attrs,0)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
init {
//初始化方法
}
override fun onDraw(canvas: Canvas) {
}
}
或者
class WaveViewKotlin: View{
constructor(context: Context?, attrs: AttributeSet?) :super(context,attrs)
init {
//初始化方法
}
override fun onDraw(canvas: Canvas) {
}
}
方式二
其他一个参数和三个参数的不可行
class WaveViewKotlin(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
init {
//初始化方法
}
override fun onDraw(canvas: Canvas) {
}
}
方式三
前提是需要先将工程的build中配置好kotlin的插件。
class WaveViewKotlin @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr){
init {
//初始化方法
}
override fun onDraw(canvas: Canvas) {
}
}
其他
作者:小空和小芝中的小空
转载说明-务必注明来源:https://zhima.blog.csdn.net/
这位道友请留步️,我观你气度不凡,谈吐间隐隐有王者霸气,日后定有一番大作为!!!旁边有点赞收藏今日传你,点了吧,未来你成功️,我分文不取,若不成功️,也好回来找我。
温馨提示:点击下方卡片获取更多意想不到的资源。
边栏推荐
- Consumer and producer cases of inter thread synchronization (condition variable)
- Function and implementation of closures
- Inter thread synchronization semaphore control
- 心樓:華為運動健康的七年築造之旅
- XML usage and parsing of data storage and transmission files
- Interviewer: your resume says you are proficient in mysql, so you say cluster / Union / overlay index, table return, index push down
- Reverse series to obtain any wechat applet code
- Uniapp converts graphic verification codes in the form of file streams into images
- The release of autok3s v0.5.0 continues to be simple and friendly
- DOM event flow, event delegate
猜你喜欢
Create raspberry PI image file of raspberry pie

Uncover gaussdb (for redis): comprehensive comparison of CODIS

Coredata data persistence

Read the configuration, explain the principle and read the interview questions. I can only help you here...
Check whether the port number is occupied

Stop "outsourcing" Ai models! The latest research finds that some "back doors" that undermine the security of machine learning models cannot be detected

Sleep formula: how to cure bad sleep?

Rxjs TakeUntil 操作符的学习笔记

揭秘GaussDB(for Redis):全面对比Codis

Principle analysis of ThreadLocal source code
随机推荐
Why does golang's modification of slice data affect the data of other slices?
JS add custom attributes to elements
Geographic location data storage scheme - redis Geo
cmd。。。。。。
ES6 deconstruction assignment rename
Flutter textfield setting can input multiple lines
leetcode-8. 字符串转换整数 (atoi)
Alvaria announces Jeff cotten, a veteran of the customer experience industry, as its new CEO
_ 17 collection overview
After flutter was upgraded from 2.2.3 to 2.5, the compilation of mixed projects became slower
Nsurlsession learning notes (III) download task
error Parsing error: Unexpected reserved word ‘await‘.
转换Cifar10数据集
Load local cifar10 dataset
What can NFT metauniverse development do?
Time wheel and implementation analysis of time wheel in go zero
Overall MySQL architecture and statement execution process
Common APIs and exception mechanisms
NFT元宇宙发展能做什么?
How to reload the win10 app store?