当前位置:网站首页>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/
这位道友请留步️,我观你气度不凡,谈吐间隐隐有王者霸气,日后定有一番大作为!!!旁边有点赞收藏今日传你,点了吧,未来你成功️,我分文不取,若不成功️,也好回来找我。
温馨提示:点击下方卡片获取更多意想不到的资源。
边栏推荐
- 什么是骨干网
- Lecun predicts AgI: big model and reinforcement learning are both ramps! My "world model" is the new way
- Swift responsive programming
- DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection翻译
- After flutter was upgraded from 2.2.3 to 2.5, the compilation of mixed projects became slower
- GO语言-锁操作
- Based on neural tag search, the multilingual abstracts of zero samples of Chinese Academy of Sciences and Microsoft Asiatic research were selected into ACL 2022
- 20省市公布元宇宙路线图
- Dino: Detr with improved detecting anchor boxes for end to end object detection
- Educational administration system development (php+mysql)
猜你喜欢

Bugly hot update usage

Detailed explanation of IVX low code platform series -- Overview (I)

mysql整体架构和语句的执行流程
Why does golang's modification of slice data affect the data of other slices?
Consumer and producer cases of inter thread synchronization (condition variable)

Vscode有什么好用的插件?

The release of autok3s v0.5.0 continues to be simple and friendly

教务系统开发(PHP+MySQL)

Uncover gaussdb (for redis): comprehensive comparison of CODIS

Reverse series to obtain any wechat applet code
随机推荐
MySQL installation tutorial
DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection翻译
leetcode-8. 字符串转换整数 (atoi)
Uniapp converts graphic verification codes in the form of file streams into images
The style of the mall can also change a lot. DIY can learn about it!
说下你对方法区演变过程和内部结构的理解
User login 2
Go language - what is critical resource security?
Alvaria announces Jeff cotten, a veteran of the customer experience industry, as its new CEO
心樓:華為運動健康的七年築造之旅
The release of autok3s v0.5.0 continues to be simple and friendly
DOM event flow, event delegate
Advanced SQL statement 1 of Linux MySQL database
Tensorflow loading cifar10 dataset
Create raspberry PI image file of raspberry pie
2021, committed to better development
深度学习 pytorch cifar10数据集训练「建议收藏」
GridLayout evenly allocate space
Resolve the format conflict between formatted document and eslint
iVX低代码平台系列详解 -- 概述篇(一)