LinearLayout是線性布局控件:要么橫向排布,要么豎向排布
決定性屬性:必須有的!
android:orientation:vertical (垂直方向) 、horizontal(水平方向)
常用屬性:
android:gravity------------設置的是控件自身上面的內容位置
android:layout_gravity-----設置控件本身相對於父控件的顯示位置
android:layout_weight----- 給控件分配剩余空間
豎向排布實例:
<?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" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕1" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕2" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕3" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按鈕4" /> </LinearLayout>
橫向排布實例:
<?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="horizontal" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="按鈕4" /> </LinearLayout>
嵌套線性布局:
<?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="horizontal" > <LinearLayout > .. </LinearLayout> <LinearLayout> .. </LinearLayout> </LinearLayout>