Anko 0.8-未解析的lparams参考 [英] Anko 0.8 - unresolved lparams reference
问题描述
主要问题是:lparams
只是从Anko消失了,还是我做错了什么?以下代码段无法编译:
The main question is: is lparams
simply gone from Anko, or am I doing something terribly wrong? The following snippet fails to compile:
verticalLayout {
}.lparams(width = matchParent, height = matchParent) {
topMargin = dip(10)
}
虽然可以正常工作,但没有任何问题:
While this works without any problems:
verticalLayout {
layoutParams = LinearLayout.LayoutParams(matchParent, matchParent).apply {
topMargin = dip(10)
}
}
我不太在乎第二个选项,但是在生成参数时必须指定布局类型,这可能会有点累(并且比原始解决方案还脆弱).
I wouldn't mind the second option too much, but you have to specify the layout type when generating the params, which can get a bit tiresome (and is also more brittle than the original solution).
在Anko
GitHub页面上,更改日志中,或者通过浏览最近的提交,我都没有找到任何东西.这是完整的UI类供参考:
I haven't found anything on the Anko
GitHub page, the changelog, or by skimming recent commits. Here's the full UI class for reference:
class ReviewsFragmentUi(ctx: AnkoContext<ReviewsFragment>) : AnkoComponent<ReviewsFragment> {
override fun createView(ui: AnkoContext<ReviewsFragment>) = ui.apply {
verticalLayout {
layoutParams = LinearLayout.LayoutParams(matchParent, matchParent).apply {
topMargin = dip(10)
}
}
}.view
}
相关的Gradle条目(我使用的是Kotlin 1.0.0-beta-3595):
Relevant Gradle entries (I'm using Kotlin 1.0.0-beta-3595):
ext.versions = [
anko : '0.8.1',
]
compile "org.jetbrains.anko:anko-common:$versions.anko",
compile "org.jetbrains.anko:anko-sdk21:$versions.anko",
compile "org.jetbrains.anko:anko-support-v4:$versions.anko",
compile "org.jetbrains.anko:anko-design:$versions.anko",
compile "org.jetbrains.anko:anko-appcompat-v7:$versions.anko",
compile "org.jetbrains.anko:anko-cardview-v7:$versions.anko",
compile "org.jetbrains.anko:anko-recyclerview-v7:$versions.anko",
compile "org.jetbrains.anko:anko-gridlayout-v7:$versions.anko",
作为后续问题:如果lparams
确实消失了,那么有没有比我已经在做的更好的替代品了?
As a follow-up question: if lparams
is indeed gone, then is there a more elegant replacement than what I'm already doing?
推荐答案
显然lparams
仍然存在,但不能用作最外层布局的扩展功能:
Apparently lparams
is still there, but cannot be used as an extension function for the outermost layout:
因此以下代码失败:
override fun createView(ui: AnkoContext<ReviewsFragment>) = ui.apply {
verticalLayout {
// Layout elements here
}.lparams {
// Layout params here
}
}.view
但这可以很好地编译:
override fun createView(ui: AnkoContext<ReviewsFragment>) = ui.apply {
verticalLayout {
lparams {
// Layout params here
}
// Layout elements here
verticalLayout { }.lparams {
// lparams works fine if there is a parent layout
}
}
}.view
值得注意的是,内部布局不建议使用lparams
的非tailing版本:当嵌套布局的类型不同时,它将创建LayoutParams
的错误子集.有关完整的讨论,请参见此 GitHub问题.
It's worth noting that using the non-tailing version of lparams
is discouraged for inner layouts: it will create the wrong sublass of LayoutParams
when the nested layouts are of different types. For a complete discussion, see this GitHub Issue.
这篇关于Anko 0.8-未解析的lparams参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!