Android.widget.LinearLayout
lastmod 2018-08-17created 2016-07-027 edits
Layout size
You can decide how much of the screen a layout should take up. The width and height can each be set as a number, a ratio, or the size of the layout's children.
| - | As large as the parent | As large as the data it holds | pixel or dp (dip) |
| | fill_parent | wrap_content | 480px or 320dp |
| | fill_parent | wrap_content | 800px or 533dp |
-
android:layout_width - This attribute specifies the width of the layout.
-
android:layout_height - This attribute specifies the height of the layout.
- As large as the parent (the space remaining in the parent's total size after excluding layouts written before the layout I am writing now)
- As the Android SDK version has gone up, a value called match_parent has appeared starting with SDK 2.2. It is the same as the existing fill_parent, but not every device uses SDK 2.2 or later yet, so we do not use this value. To size to the parent as before, set fill_parent.
- As large as the data it will hold
- Used when you cannot know how large the child layouts of the layout you are writing will become. Also used for text views and image views to change the layout size according to the text or image that will be entered.
- Specifying directly in pixel or dp (dip)
- A few tips for collaborating with Android designers
- Providing alternative resources (Android Providing Resource)
- You can also write a layout by specifying numbers directly. However, this approach is half recommended and half not. dp is recommended because, used well, it can provide the same UI in most cases, but pixel is strongly discouraged. As you will see if you read the related post link, using dp can be a very wise and advantageous choice when you cannot provide many Resources. If you could provide every resource, you could show the same UI on every device, but in practice that is hard.
How to apply Weight
To change the size of a View according to its weight value, the layout_width or layout_height value must be 0dp.
gravity & layout_gravity
Since both attributes contain the word gravity, you can guess that they apply something like gravity. Depending on the value of these attributes, layouts are pulled by gravity toward that position, just as we are pulled toward the ground (down).
-
android:gravity - This attribute sets where the child layouts of the layout you are currently writing are pulled by gravity.
-
android:layout_gravitiy - This attribute sets where the layout you are currently writing is pulled by gravity.
A gravity value in the parent layout can produce the same result as a layout_gravity value in the child layouts. The reason to use both anyway is that when all child layouts need the same gravity, the gravity attribute set on the parent layout is enough, but when child layouts each need a different gravity, left, right, and so on must be set on each child layout. There are 7 values in total, listed below.
Values that can be set for android:gravity and android:layout_gravity:
-
left(gravity to the left) -
right(gravity to the right) -
top(gravity to the top) -
bottom(gravity to the bottom) -
center_vertical(gravity to the vertical center) -
center_horizontal(gravity to the horizontal center) -
center(gravity to the exact center)
These 7 values can also be combined when the combination is logically consistent. (For example, gravity to the left and to the vertical center, gravity to the horizontal center and to the bottom, or gravity to the right and to the bottom at the same time.)
Below are illustrations, for various cases, of
-
gravity(the parent specifying the gravity attribute for its children) and -
layout_gravity(specifying its own gravity attribute).
Android_layout_gravity.png
orientation
In LinearLayout, the orientation attribute is very important. It determines, by default, how child views are laid out. There are two attribute values.
-
horizontal - Lay out child views (widgets) horizontally.
-
vertical - Lay out child views (widgets) vertically.
This is short because there is not much to it, but it is very important. Even if orientation is not set, LinearLayout has the attribute value horizontal as its default.
layout_weight
When building layouts in Android, the layout_weight attribute lets you do a lot. Because it automatically stretches, shrinks, and fills layouts, using the layout_weight attribute lets you provide a UI that does not break on devices with many different resolutions. If you specify fixed numbers, a device with a different resolution from the one you tested on may show a UI the developer did not intend, and the layout_weight attribute can solve this.
background
When using a background image, you can set it through this attribute. The single android:background attribute can set several things.
- Setting a color:
android:background="#FFFFFF" - Setting a background image:
android:background="@drawable/이미지파일이름"(sets the background from an image resource inres/drawable) - Button click gestures, changing the button image by state, building it as an xml file
See also
Favorite site
- Understanding LinearLayout properly 1
- Controlling Android LinearLayout attributes
- How to use the LinearLayout container
References
-
Hedge's_Thought_-_Android_understanding_LinearLayout.pdf ↩