Android Anko忽略样式中定义的布局_边距

Android Anko忽略样式中定义的布局_边距,android,kotlin,android-styles,anko,Android,Kotlin,Android Styles,Anko,我创建了一个自定义样式: <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 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>
当我在布局中使用此选项时:

static { text = resources.getString(R.string.name) }
将忽略marginEnd值

如果我在anko中手动添加边距:

static { text = resources.getString(R.string.name) }.lparams { marginEnd = dip(5) }
利润很好


你们知道为什么anko会忽略我的边距值,或者用其他方式为扩展视图anko函数定义预定义的边距吗?

这不是anko的问题,Android就是这样工作的:

如果要在自定义样式中指定布局\ U边距,则必须将此样式显式应用于希望具有指定边距的每个视图,如下面的代码示例所示。在主题中包含此样式并将其应用于应用程序或活动将不起作用

这是因为以layout_u开头的属性是LayoutParams,或者如本例中的MarginLayoutParams。每个视图组都有自己的LayoutParams实现。因此,版面边距不仅仅是可以应用于任何地方的一般属性。它必须应用在视图组的上下文中,该视图组专门将其定义为有效参数


寻找更多信息。

这不是安科的问题,这是安卓的工作原理:

如果要在自定义样式中指定布局\ U边距,则必须将此样式显式应用于希望具有指定边距的每个视图,如下面的代码示例所示。在主题中包含此样式并将其应用于应用程序或活动将不起作用

这是因为以layout_u开头的属性是LayoutParams,或者如本例中的MarginLayoutParams。每个视图组都有自己的LayoutParams实现。因此,版面边距不仅仅是可以应用于任何地方的一般属性。它必须应用在视图组的上下文中,该视图组专门将其定义为有效参数


查看更多信息。

正如@John在回答中指出的,使用样式并不是定义布局参数的选项

因此,我开发了一个函数,在applyRecursive中使用,该函数迭代视图并应用我想要应用的布局

解决方案:

我想为宽度和高度定义matchParent,为TableView定义16dp的边距,所以我创建了一个扩展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)
                }
            }
        }
    }
}
要使用该函数,在视图定义中,我只需将其以安全方式传递到应用程序中:

verticalLayout {
        tableViewFrame {
            tableRow {
                ...
            }
        }
    }
}.applyRecursively { view -> applyTemplateViewLayouts(view) }

我在medium上写了一篇文章,其中有更详细的解释:

正如@John在他的回答中指出的,使用样式不是定义布局参数的选项

因此,我开发了一个函数,在applyRecursive中使用,该函数迭代视图并应用我想要应用的布局

解决方案:

我想为宽度和高度定义matchParent,为TableView定义16dp的边距,所以我创建了一个扩展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)
                }
            }
        }
    }
}
要使用该函数,在视图定义中,我只需将其以安全方式传递到应用程序中:

verticalLayout {
        tableViewFrame {
            tableRow {
                ...
            }
        }
    }
}.applyRecursively { view -> applyTemplateViewLayouts(view) }
我在媒体上写了一篇文章,其中有更详细的解释: