Search code examples
androidandroid-layoutandroid-custom-viewandroid-custom-attributes

Define custom ViewGroup attributes for children


I am looking for a way to define custom attributes for views that are used by their children. For example layout_centerInParent for children of RelativeLayouts or layout_span for children of TableLayouts.


Solution

  • Just in case someone else gets here hoping for a direction like I did:

    The layout parameters that are defined in the children are used in the parents. For example, layout_centerInParent is used by the RelativeLayout parent to position the child according to its value of this attribute.

    In a custom ViewGroup you can create an inner class that extends ViewGroup.LayoutParams. Then you can use XML attributes within the children and retrieve them in the custom layout using the View#getLayoutParams() method.

    You can look at this question and its answer.