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
.
Asked
Active
Viewed 1,408 times
6

Kuno
- 3,492
- 2
- 28
- 44
-
why do you think they ar used by children and not where they are defined? – pskink Mar 15 '15 at 13:10
-
I am pretty sure there was a time when using attributes that were declared in the parent led to a lint error. I just tried it again without problems. – Kuno Mar 15 '15 at 14:02
1 Answers
13
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.