Why do layout options work in Android programming?

First in my background: I'm new to Java coming out of Ruby. If it helps.

I am confused about how layout options work. I follow the fundamental initiatives of Hello World to create an Android application. Step 1, extend the Activity class and the onCreate () method to access the XML layout. OK I understood.

Then I create a layout (say RelativeLayout) in Main.XML. So this uses the RelativeLayout class, which extends the ViewGroup class, for now, is fine. Then let me say that I create a button inside this. This is where my question begins. If I look at the example that I follow, I see the attributes assigned to a button belonging to the RelativeLayout class (ie: android: layout_alignParentRight = "true"). These seem to be layout options. But why does this work? The button class seems to be inherited from the View class. Why can a button object accept attributes for a RelativeLayout object? Maybe my Ruby programming is confusing to me.

Thanks!

Update: for posterity: thanks to Slothsberry for pointing out the XML Layouts link, which seems to clearly describe the answer in the two sections “Attributes” and “Layout Parameter”. The attribute section reads:

Each View and ViewGroup object supports its own XML attribute. Some attributes are specific to the View object (for example, TextView supports the textSize attribute), but these attributes are also inherited by any View objects that the class can. Some of them are common to all View objects, since they are inherited from the root View class (for example, the id attribute). And other attributes are considered "layout parameters", which are attributes that describe specific orientations of the location of the View object, as defined by this object by the parent ViewGroup object.

The layout options section, although perhaps this is the section that really answers this question. Where does he indicate:

Each ViewGroup class implements a nested class that extends ViewGroup.LayoutParams. This subclass contains property types that determine the size and position for each child view, depending on the viewing group. As you can see in Figure 1, the parent view group defines layout options for each child view (including the child view group).

They also give a good chart. It seems like a novice programmer needs to admit that although references to Java classes are referenced, XML acts more like a CSS sheet, and these attributes are first computed in a nested manner before being computed and transferred to their Java class mappings. That my current understanding is anyway :)

+7
source share
4 answers

Layout options do not strictly reflect object inheritance (as you noticed). The reason is that there are two parts to the layout: customizing the view and parameterizing the parent of the view using this view as an argument.

That way, options like android: layout_below will be ignored if the parent layout is not RelativeLayout. From an OOP perspective, it might make sense to put this parameter in a RelativeLayout object. But so you can do it in Java code.

The XML code uses an approach whereby information about a child is contained in a child element. layout options that require a parent who is not present will be ignored when the layout is bloated. Its good android system is used to make XML more readable and portable. And this does not apply strictly to the structure of the class package, but rather to the intuitive way that people think about placing things in the layout.

+6
source

All layout elements in android inherit from View, although many are indirect.

The general View class has properties (attributes) that correspond to any element of the visible layout. for the root layout, some systems, such as Gravity Layout, Layout Dimensions, etc. installed by the system (in most cases I think).

If my root layout is a linear layout, Android will allow me to have a relative layout as the root. Android will allow me to set various layout properties for a nested element to control how it displays. This works the same for Button and any other Android layout.

If you do not care about a particular property, do not set it. They are present so that you can control the screens of your application. Browse XML layouts or Hello Views to get you started.

+3
source

You are a little confused that a certain XML object does not belong in the layout parameter. If you put it in one child XML of XXXView or XXXLAyout, it will understand that its right side should be in the same place as the parent right.

Then, if you do not create layout parameters for this child, the child will try to inherit its parent elements.

+1
source

Layout

Layout is a two-pass process: transfer skipping and layout. An intermediate pass is implemented in measure(int, int) and is a reverse look at the view tree. Each view pushes the size specifications to the tree during the recursion. At the end of the passage of the measure, each species stores its measurements. The second pass happens in layout(int, int, int, int) , and also from top to bottom. During this passage, each parent is responsible for placing all their children using the dimensions calculated in the measure passage.

When the view () method returns a value, its getMeasuredWidth() and getMeasuredHeight() values ​​must be set along with the values ​​for all of these view children. Presented measured widths and measured heights should take into account the restrictions imposed by the parents of the gaze. This ensures that at the end of the event, all parents take all the measurements of their children. A parent view can repeatedly call a measure () on its children. For example, a parent can measure each child once with indefinite measurements to find out how big they are, and then call measure () again with real numbers if the sum of all child unlimited sizes is too large or too small.

Skipping measures uses two classes to pass measurements. The View.MeasureSpec class View.MeasureSpec used by views to tell parents how they want to be measured and positioned. The base class LayoutParams simply describes how large the view should be both in width and in height. For each dimension, he can indicate one of:

  • exact number
  • MATCH_PARENT, which means that the view wants to be as large as its parent (minus padding)
  • WRAP_CONTENT, which means the view wants to be large enough to add its contents (plus an addition).

There are subclasses of LayoutParams for different subclasses of ViewGroup. For example, AbsoluteLayout has its own subclass of LayoutParams, which adds the values ​​of X and Y.

MeasureSpecs are used to push tree requirements from parent to child. A MeasureSpec can be in one of three modes:

  • UNSPECIFIED: Used by the parent to determine the desired size of the baby species. For example, LinearLayout can call measure () on its child with a height set to UNSPECIFIED and a width EXACTLY 240 to find out how tall a child should be given a width of 240 pixels.
  • EXACTLY: this is used by the parent to impose the exact size on the child. The child must use this size and ensure that all of his descendants comply with this size.
  • AT_MOST: used by the parent to overlay the maximum size on the child. The child must guarantee that he and all his descendants will be consistent with this size.

To start linking, call requestLayout() . This method is usually invoked by the idea of ​​it when it considers that it can no longer match its current boundaries.

0
source

All Articles