线性布局
LinearLayout
-
指定各个节点的排列方向
android:orientation="horizontal"
-
设置右对齐
android:layout_gravity="right"
- 当竖直布局时,只能左右对齐和水平居中,顶部底部对齐竖直居中无效
- 当水平布局时,只能顶部底部对齐和竖直居中
- 使用match_parent时注意不要把其他组件顶出去
-
线性布局非常重要的一个属性:权重
android:layout_weight="1"
权重设置的是按比例分配剩余的空间,并且要设置layout_width或layout_height要为0
|
示例:
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ff0000"
/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#ffffff"
/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#000000"
/>
<TextView
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/darker_gray"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
>
<TextView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#00ff00"
/>
<TextView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/darker_gray"
/>
<TextView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#000000"
/>
<TextView
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#ffcc0000"
/>
</LinearLayout>
</LinearLayout>