【问题标题】:How can I set value of layout_weight of a radio button in Anko DSL (Android)?如何在 Anko DSL (Android) 中设置单选按钮的 layout_weight 值?
【发布时间】:2018-03-15 07:51:47
【问题描述】:

我使用 Anko DSL 创建 UI 而不是 XML。但是当我要以 Anko 方式设置单选按钮的 layout_weight 参数时,我遇到了错误。

我尝试了以下方法:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    verticalLayout() {
        radioGroup() {
            orientation = LinearLayout.HORIZONTAL
            radioButton {
                id = RADIO_SECOND
                text = "second(s)"

            }.lparams(width = wrapContent, height = wrapContent, weight = 0.25F)

            // Few more radio button
        }
    }
}

但它给出了错误Error:(107, 19) 'inline fun <T : View> RadioButton.lparams(width: Int = ..., height: Int = ..., weight: Float): RadioButton' can't be called in this context by implicit receiver. Use the explicit one if necessary

我该如何继续?

【问题讨论】:

  • 你在什么上下文中使用它?那么基本上你的 sn-p 周围有什么代码?
  • @creativecreatorormaybenot 我已经用周围的代码更新了帖子。

标签: android kotlin kotlin-android-extensions anko


【解决方案1】:

通过指定radioButtonweight 参数,您选择使用在Anko 的_LinearLayout 类上定义的lparams 函数,因此您基本上是在尝试给radioButton 一个在 verticalLayout 的上下文中的权重你已经包裹了你的整个布局。

要在radioGroup 的上下文中赋予它权重,您可以使用另一个lparams 函数,其类似的参数名为initWeight

verticalLayout {
    radioGroup {
        orientation = LinearLayout.HORIZONTAL
        radioButton {
            id = RADIO_SECOND
            text = "second(s)"
        }.lparams(width = wrapContent, height = wrapContent, initWeight = 0.25F)
    }
}

这会将调用置于正确的上下文中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-11
    • 2011-05-07
    • 2021-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    • 2021-03-07
    • 1970-01-01
    相关资源
    最近更新 更多