Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 464
Kuno Avatar asked Mar 15 '15 12:03

Kuno


People also ask

What is a ViewGroup?

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.

What is customized view?

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.


1 Answers

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.

like image 85
et_l Avatar answered Oct 19 '22 10:10

et_l