我想创建一个自定义视图,这只是一些Android视图的包装器。我考虑过创建一个自定义的ViewGroup来管理它的子视图的布局,但我不需要这么复杂。我基本上想要做的是:
class MainActivity
verticalLayout {
textView {
text = "Something that comes above the swipe"
}
swipeLayout {
}
}
class SwipeLayout
linearLayout {
textView {
text = "Some text"
}
textView {
text = "Another text"
}
}原因是我想把SwipeLayout代码移到一个单独的文件中,但不想自己做任何复杂的布局工作。使用Anko可以做到吗?
编辑:按照建议,如果视图是根布局,则Is it possible to reuse a layout in Kotlin Anko可以解决此问题。但如示例所示,我希望将其包含在另一个布局中。这有可能吗?
发布于 2016-12-14 10:38:20
您可以使用ViewManager。
fun ViewManager.swipeLayout() = linearLayout {
textView {
text = "Some text"
}
textView {
text = "Another text"
}
}class MainActivity
verticalLayout {
textView {
text = "Something that comes above the swipe"
}
swipeLayout {}
}发布于 2016-12-05 18:21:11
我也在寻找这样的东西,但我为自定义视图找到的最优解决方案是这样的:
public inline fun ViewManager.customLayout(theme: Int = 0) = customLayout(theme) {}
public inline fun ViewManager.customLayout(theme: Int = 0, init: CustomLayout.() -> Unit) = ankoView({ CustomLayout(it) }, theme, init)
class CustomLayout(c: Context) : LinearLayout(c) {
init {
addView(textView("Some text"))
addView(textView("Other text"))
}
}https://stackoverflow.com/questions/40147360
复制相似问题