Anko相当于xml中的样式属性
我需要创建无边界按钮。 在XML中最简单的方法来设置Widget.AppCompat.Button.Borderless
。 我试图用Anko
做这个
button(theme = R.style.Widget_AppCompat_Button_Borderless_Colored, text = "Send") { horizontalGravity = Gravity.END lparams(wrapContent, wrapContent) }
但是没有效果。 我究竟做错了什么?
尝试使用接受attr
资源的第三个构造函数参数:
addView(Button(activity, null, R.attr.borderlessButtonStyle))
另外,您可以将其声明为DSL组件:
fun ViewManager.borderlessButton(textRes: Int = 0) = borderlessButton(textRes) { } fun ViewManager.borderlessButton(textRes: Int = 0, init: Button.()->Unit) = ankoView({ Button(it, null, R.attr.borderlessButtonStyle) }, 0) { if (textRes != 0) setText(textRes) init() }
那么,你的呼叫站点可能是这样的:
borderlessButton(android.R.string.ok)
您可以查看以类似的方式声明的Anko的horizontalProgressBar
方法和HORIZONTAL_PROGRESS_BAR_FACRTORY
。
顺序是错误的。 尝试这个:
button("Your text", android.support.design.R.style.Base_Widget_AppCompat_Button_Borderless_Colored) { //your params }
这对我有用。
- 如何使用kotlin将layoutmanager设置为RecycleView
- 无法在Kotlin上设置ViewModel
- 在Project中不使用Kotlin编译项目
- Android室 – 使用LIKE选择查询
- Android DataBinding:Kotlin中的@BindingAdapter不识别lambda表达式
- 使用Kotlin进行数据绑定会导致Resources $ NotFoundException
- Android工作室插件ID:'kotlin-android-extensions'
- Kotlin android扩展,软件包不能被导入
- TaskStackBuilder addParentStack()方法不起作用