为子项定义自定义ViewGroup属性

时间:2015-03-15 12:47:37

标签: android android-layout android-custom-view android-custom-attributes

我正在寻找一种为子女使用的视图定义自定义属性的方法。例如layout_centerInParentRelativeLayouts的儿童layout_spanTableLayouts的子女为{{1}}。

1 个答案:

答案 0 :(得分:10)

以防万一其他人到这里希望像我这样的方向:

子项中定义的布局参数在父项中使用。例如,layout_centerInParent父级使用RelativeLayout根据子属性的值来定位子级。

在自定义ViewGroup中,您可以创建扩展ViewGroup.LayoutParams的内部类。然后,您可以在子项中使用XML属性,并使用View#getLayoutParams()方法在自定义布局中检索它们。

您可以查看this question and its answer