如何为 activity 自动导入 import kotlinx.android.synthetic.main.xxx

How to auto import import kotlinx.android.synthetic.main.xxx for activity

Kotlin 具有使用 import kotlinx.android.synthetic.main.activity_main.* 进行视图绑定的强大功能。我还知道 Android Studio 功能强大且可灵活定制。 为了使用视图绑定,我们需要在创建 activity.

之后手动包含 kotlinx.android.synthetic.main.activity_xxx.*

Android Studio 中是否有任何方式/自定义选项,以便每当创建新的 activity 时,它会自动导入相应的 kotlinx.android.synthetic.main.activity_xxx.*?例如,如果我正在创建 NewActivity,它应该已经有 kotlinx.android.synthetic.main.activity_new.*

Android Studio 应该 auto-import 它本身或提示您这样做。如果还没有,请检查 IDE 的 Kotlin 版本是否与您的项目匹配(在 gradle 中)并重新启动 IDE.

我遇到了类似的问题。不过我是新手,所以为了帮助其他新手,我会添加这个。原来我没有安装所有的插件。

默认生成的项目是:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

但还需要包括扩展位:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id "kotlin-android-extensions"  // The secret sauce that allows an implicit findViewById.
}