在Anko DSL中创建自定义View/ViewGroup类
我想创建一个自定义视图,它只是一些Android视图的包装.我考虑创建一个自定义ViewGroup,以管理其子视图的布局,但是我不需要这种复杂性.我基本上想做的是这样的:
I want to create a custom View which is just a wrapper of some Android Views. I looked into creating a custom ViewGroup which manages the layout of it's child views, but I don't need such complexity. What I basically want to do is something like:
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可以做到吗?
The reason is that I'd like to move the SwipeLayout code into a separate file but don't want to do any complex layout stuff myself. Is this possible using Anko?
根据建议,如果视图是根布局,是否可以在Kotlin Anko中重用布局解决了此问题.但是,如示例所示,我想将其包含在另一个布局中.有可能吗?
As suggested, Is it possible to reuse a layout in Kotlin Anko solves this problem if the view is a root layout. But as shown in the example, I'd like to include this within another layout. Is that possible?
您可以使用ViewManager.
you can use 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 {}
}