哈?kotlin-android-extensions被弃用了???
前言
今天阅读了郭霖的《第一行代码》第三版,很惊讶地发现获取控件的实例竟然不需要像以前那样findViewById了,通过’kotlin-android-extensions’插件就可以直接通过控件的id来获取控件实例。
举个栗子:
1 | package com.example.layouttest1 |
然而此时,Android Studio下面的Build Output有一条Warning:
1 | The 'kotlin-android-extensions' Gradle plugin is deprecated. Please use this migration guide (https://goo.gle/kotlin-android-extensions-deprecation) to start working with View Binding (https://developer.android.com/topic/libraries/view-binding) and the 'kotlin-parcelize' plugin. |
???? 这么方便的功能这么就被弃用了呢??
在此就不细究是什么原因导致了官方弃用’kotlin-android-extensions’,让我们来康康有什么其它可以省去findViewById的解决方案。
Solution: ViewBinding
1.在build.gradle中配置ViewBinding
打开build.gradle,添加
1 | buildFeatures { |
2.在Activity中使用ViewBinding
1 | package com.example.layouttest1 |
OK!以上就是ViewBinding的基本使用~
Author: 东南dnf
License: 本博客所有文章除特别声明外,均采用许可协议 CC-BY-NC-4.0 转载请注明出处!