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
.
View or ViewGroup subclasses are used to build the GUI elements. View is a basic building block of UI (User Interface) in android. A view is a small rectangular box that responds to user inputs. Eg: EditText, Button, CheckBox, etc. ViewGroup is an invisible container of other views (child views) and other ViewGroup.
A well-designed custom view is much like any other well-designed class. It encapsulates a specific set of functionality with an easy to use interface, it uses CPU and memory efficiently, and so on. In addition to being a well-designed class, though, a custom view should: Conform to Android standards.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With