当前位置:网站首页>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/
这位道友请留步️,我观你气度不凡,谈吐间隐隐有王者霸气,日后定有一番大作为!!!旁边有点赞收藏今日传你,点了吧,未来你成功️,我分文不取,若不成功️,也好回来找我。
温馨提示:点击下方卡片获取更多意想不到的资源。
边栏推荐
- Shuttle pop-up returns to the upper level
- Lecun predicts AgI: big model and reinforcement learning are both ramps! My "world model" is the new way
- Mixed density network (MDN) for multiple regression explanation and code example
- Take you to the open source project of smart home: the preliminary configuration of zhiting home cloud and home assistant+ homebridge
- 商城风格也可以很多变,DIY 了解一下!
- 心樓:華為運動健康的七年築造之旅
- Div element
- About the use of Aidl, complex data transmission
- 读配置、讲原理、看面试真题,我只能帮你到这了。。。
- This article will help you understand the common concepts, advantages and disadvantages of JWT
猜你喜欢

Go language - lock operation

Vscode有什么好用的插件?

Nsurlsession learning notes (III) download task

10款超牛Vim插件,爱不释手了

赫尔辛基交通安全改善项目部署Velodyne Lidar智能基础设施解决方案

一行代码可以做什么?

不要小看了积分商城,它的作用可以很大!

AutoK3s v0.5.0 发布 延续简约和友好

Coredata data persistence

DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection翻译
随机推荐
Final, override, polymorphic, abstract, interface
赫尔辛基交通安全改善项目部署Velodyne Lidar智能基础设施解决方案
Deadlock, thread communication, singleton mode
The textfield is encapsulated by the flutter itself, which causes the data display to be disordered when the data in the list is updated.
20省市公布元宇宙路线图
加载本地cifar10 数据集
Read AFN through - from the creation of manager to the completion of data parsing
SQL最常用的语句
What can one line of code do?
Power representation in go language
What processes are needed to build a wechat applet from scratch?
Helsinki traffic safety improvement project deploys velodyne lidar Intelligent Infrastructure Solution
Sleep formula: how to cure bad sleep?
cmd。。。。。。
mysql整体架构和语句的执行流程
Converting cifar10 datasets
[issue 24] one year experience of golang to develop futu
flutter
普通人的2022春招总结(阿里、腾讯offer)
ES6 deconstruction assignment rename