1. Overview : LinearLayout arranges containers one by one linearly
2. XML attributes
xml attribute | Related methods | effect |
---|---|---|
android:gratity | setGravity | Set the alignment, this attribute supports multiple attribute values, can be connected with vertical bars |
android:orientation | setOrientation | Set the arrangement direction of the components in the container, horizontal (horizontal default) or vertical (vertical) |
3. Case
- Strings Strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="btn1">按钮1</string>
<string name="btn2">按钮2</string>
<string name="btn3">按钮3</string>
<string name="btn4">按钮4</string>
<string name="btn5">按钮5</string>
</resources>
- Re-edit the layout file activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal" à线性布局对齐方式
android:orientation="vertical" > à线性布局布局方向
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn2" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn3" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn4" />
<Button
android:id="@+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn5" />
</LinearLayout>