首页 > 解决方案 > 如果其他情况,Android kotlin 导入合成

问题描述

有什么方法可以导入合成布局,例如:

If (App.layout1) import ...layout1 else layout2

?

我基本上需要这个功能;)谢谢

标签: androidkotlin

解决方案


我知道你可以在 kotlin 中重命名导入

import kotlinx.android.synthetic.activity1.someView as value1
import kotlinx.android.synthetic.activity2.someView as value2

但这可能会导致您手动手动导入所有视图。(未测试)也许您可以导入整个合成类而不是所有视图

import kotlinx.android.synthetic.activity1 as views1
import kotlinx.android.synthetic.activity2 as views2

接着

if (...) views1.someView.stuff() else views2.someView.stuff()

可能不支持条件导入,因为 Java 不支持它。


推荐阅读