是否可以在Kotlin Anko中重用布局 [英] Is it possible to reuse a layout in Kotlin Anko

查看:127
本文介绍了是否可以在Kotlin Anko中重用布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到使用Anko的最大好处是其可重用性.但是我找不到确切的例子.

I read that the most benefit of using Anko is its reusability. But i could't find its exact example.

当前在新的Android布局系统中,样板如下:

Currently in the new Android layout system, the boiler plate is like below:

DrawerLayout (with some setup)
   CoordinatorLayout (with some setup)
      AppBarLayout (with some setup)
         ToolBar
      <The Main Content>
   NavigationView (with header inflated)

从上面的布局结构来看,只有<The Main Content>是变量.和 在许多情况下,这些仪式设置几乎在每次活动中都重复.

From the layout structure above, only <The Main Content> is varry. And in many cases those ceremonial setup duplicated almost in every activity.

因此,在这里,Anko即时通讯正在考虑是否存在有关该问题的可重用解决方案.我不希望它可用于通用布局,但至少我可以最小化项目中的礼节性代码.也许我需要类似的东西:

So here with Anko im thinking if there is a reusable solution about that issue. Im not expecting it will be reusable for general purpose layout, but et least i can minimize the ceremonial code in the project. Maybe i need something like:

class MainUI: AnkoComponent<MainActivity> {
  override fun createView(ui: AnkoContext<MainActivity>): View{
     return with(ui) {
        myCustomRootLayout {
           //here is what <The Main Content> will be
        }
     }
  }
}

从上面的代码中,我期望myCustomRootLayout将完成根布局的所有仪式设置,例如(DrawerLayout,CoordinatorLayout等).

From the code above im expecting myCustomRootLayout will do all the ceremonial setup for the root layout such as (DrawerLayout, CoordinatorLayout etc etc).

有可能吗?

编辑 所以我认为我的问题是:如何制作可以托管其他组件的自定义组件

EDIT So i think my question is: How to make a custom component which can host other component

推荐答案

重用代码的一种方法是简单地将myCustomRootLayout提取到扩展方法中,如下所示:

One way to reuse the code is to simply extract myCustomRootLayout into a extension method like so:

class MainUI: AnkoComponent<MainActivity> {
    override fun createView(ui: AnkoContext<MainActivity>): View {
        return with(ui) {
            myCustomRootLayout {
               recyclerView()
            }
        }
    }
}

fun <T> AnkoContext<T>.myCustomRootLayout(customize: AnkoContext<T>.() -> Unit = {}): View {
    return relativeLayout {
        button("Hello")
        textView("myFriend")
        customize()
    }
}

不过,文档中所述的 :

尽管您可以直接使用DSL(在onCreate()中或在任何地方 其他),而无需创建任何额外的类,通常很方便 在单独的类中具有UI.如果您使用提供的AnkoComponent 界面,您还可以免费获得DSL布局预览功能.

Although you can use the DSL directly (in onCreate() or everywhere else), without creating any extra classes, it is often convenient to have UI in the separate class. If you use the provided AnkoComponent interface, you also you get a DSL layout preview feature for free.

将可重复使用的片段提取到单独的AnkoComponent中似乎是一个好主意:

It seems to be a good idea to extract the reusable piece into separate AnkoComponent:

class MainUI : AnkoComponent<MainActivity> {
    override fun createView(ui: AnkoContext<MainActivity>): View {
        return with(ui) {
            MyCustomRootLayout<MainActivity>({
                recyclerView()
            }).createView(ui)
        }
    }
}


class MyCustomRootLayout<T : Context>(val customize: AnkoContext<T>.() -> Unit = {}) : AnkoComponent<T> {
    override fun createView(ui: AnkoContext<T>) = with(ui) {
        relativeLayout {
            button("Hello")
            textView("myFriend")
            customize()
        }
    }
}

这篇关于是否可以在Kotlin Anko中重用布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆