在Anko DSL中创建自定义View/ViewGroup类 [英] Create a custom View/ViewGroup class in Anko DSL

查看:195
本文介绍了在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 {}
}

这篇关于在Anko DSL中创建自定义View/ViewGroup类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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