【问题标题】:How to set FloatingActionButton border width with Anko如何使用 Anko 设置 FloatingActionButton 边框宽度
【发布时间】:2017-11-16 09:54:31
【问题描述】:

我刚刚从 Java 切换到 Kotlin,并且正在使用 Anko 构建 layouts

这是我的布局:

relativeLayout {

    floatingActionButton {
        imageSource = R.drawable.kotlin_is_amazing
    }.lparams {
        width = wrapContent
        height = wrapContent
    }

}

现在的问题是如何用 Anko 设置app:borderWidth

【问题讨论】:

    标签: android android-layout kotlin anko


    【解决方案1】:

    如果视图没有根据setBorderWidth的方法,可以通过style来实现。

    1. 声明将设置此参数的样式。

      <style name="whatever">
          <item name="app:setBorderWidth">2dp</item>
      </style>
      
    2. 在属性中引用此样式。

      <attr name="whateverRef" format="reference">@style/whatever</attr>
      
    3. 将此属性作为defStyleAttr 传递给查看构造函数。

      FloatingActionButton(context, null, R.attr.whateverRef)
      
    4. 扩展 Anko DSL。

      val whateverFactory = { ctx: Context ->
          FloatingActionButton(ctx, null, R.attr.whateverRef)
      }
      
      inline fun ViewManager.whateverFab() =
              ankoView(whateverFactory, 0, {})
      
      inline fun ViewManager.whateverFab(configure: FloatingActionButton.() -> Unit) =
              ankoView(whateverFactory, 0, configure)
      

    【讨论】:

    • 谢谢你的回答,其实我已经切换回xml布局了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-12
    • 2011-10-25
    相关资源
    最近更新 更多