Anko 忽略 layout_margin 定义的样式
Anko ignoring layout_margin defined in style
我创建了自定义样式:
<style name="Static">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginEnd">5dp</item>
</style>
然后我用静态函数扩展了anko:
inline fun ViewManager.static(theme: Int = R.style.Static, init: TextView.() -> Unit) = ankoView(::TextView, theme, init)
当我在布局中使用它时:
static { text = resources.getString(R.string.name) }
marginEnd 值被忽略。
如果我在 anko 中手动添加边距:
static { text = resources.getString(R.string.name) }.lparams { marginEnd = dip(5) }
边距还可以。
你们知道 anko 忽略了我的边距值或任何其他方式来为扩展视图 anko 函数定义预定义边距吗?
这不是 Anko 问题,Android 是这样工作的:
If you are specifying layout_margin in a custom style, this style must be explicitly applied to each individual view that you wish to have the specified margin (as seen in the code sample below). Including this style in a theme and applying it to your application or an activity will not work.
这是因为以 layout_ 开头的属性是 LayoutParams
,或者在本例中是 MarginLayoutParams
。每个 ViewGroup
都有自己的 LayoutParams
实现。因此 layout_margin
不仅仅是可以在任何地方应用的一般属性。它必须在明确定义为有效参数的 ViewGroup
上下文中应用。
查看 here 了解更多。
正如@John 在他的回答中指出的那样,使用样式不是定义布局参数的选项。
因此,我开发了一个在 applyRecursively 中使用的函数,它迭代视图并应用我想要应用的布局。
解决方法:
我想为 TableView 定义宽度和高度的 matchParent 和 16dp 的边距,所以我创建了一个新的 class 来扩展 TableLayout
class TableViewFrame(context: Context) : TableLayout(context)
然后在函数中当视图是 TableViewFrame 时应用我的布局
fun applyTemplateViewLayouts(view: View) {
when(view) {
is TableViewFrame -> {
when(view.layoutParams) {
is LinearLayout.LayoutParams -> {
view.layoutParams.height = matchParent
view.layoutParams.width = matchParent
(view.layoutParams as LinearLayout.LayoutParams).margin = view.dip(16)
}
}
}
}
}
要使用该函数,在视图定义中,我只是在 applyRecursively 中传递它:
verticalLayout {
tableViewFrame {
tableRow {
...
}
}
}
}.applyRecursively { view -> applyTemplateViewLayouts(view) }
我在medium写了一篇文章,解释的比较详细:https://medium.com/@jonathanrafaelzanella/using-android-styles-with-anko-e3d5341dd5b4
我创建了自定义样式:
<style name="Static">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginEnd">5dp</item>
</style>
然后我用静态函数扩展了anko:
inline fun ViewManager.static(theme: Int = R.style.Static, init: TextView.() -> Unit) = ankoView(::TextView, theme, init)
当我在布局中使用它时:
static { text = resources.getString(R.string.name) }
marginEnd 值被忽略。
如果我在 anko 中手动添加边距:
static { text = resources.getString(R.string.name) }.lparams { marginEnd = dip(5) }
边距还可以。
你们知道 anko 忽略了我的边距值或任何其他方式来为扩展视图 anko 函数定义预定义边距吗?
这不是 Anko 问题,Android 是这样工作的:
If you are specifying layout_margin in a custom style, this style must be explicitly applied to each individual view that you wish to have the specified margin (as seen in the code sample below). Including this style in a theme and applying it to your application or an activity will not work.
这是因为以 layout_ 开头的属性是 LayoutParams
,或者在本例中是 MarginLayoutParams
。每个 ViewGroup
都有自己的 LayoutParams
实现。因此 layout_margin
不仅仅是可以在任何地方应用的一般属性。它必须在明确定义为有效参数的 ViewGroup
上下文中应用。
查看 here 了解更多。
正如@John 在他的回答中指出的那样,使用样式不是定义布局参数的选项。
因此,我开发了一个在 applyRecursively 中使用的函数,它迭代视图并应用我想要应用的布局。
解决方法:
我想为 TableView 定义宽度和高度的 matchParent 和 16dp 的边距,所以我创建了一个新的 class 来扩展 TableLayout
class TableViewFrame(context: Context) : TableLayout(context)
然后在函数中当视图是 TableViewFrame 时应用我的布局
fun applyTemplateViewLayouts(view: View) {
when(view) {
is TableViewFrame -> {
when(view.layoutParams) {
is LinearLayout.LayoutParams -> {
view.layoutParams.height = matchParent
view.layoutParams.width = matchParent
(view.layoutParams as LinearLayout.LayoutParams).margin = view.dip(16)
}
}
}
}
}
要使用该函数,在视图定义中,我只是在 applyRecursively 中传递它:
verticalLayout {
tableViewFrame {
tableRow {
...
}
}
}
}.applyRecursively { view -> applyTemplateViewLayouts(view) }
我在medium写了一篇文章,解释的比较详细:https://medium.com/@jonathanrafaelzanella/using-android-styles-with-anko-e3d5341dd5b4