Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 線性佈局 LinearLayout 用法詳解

編輯:關於 Android 程式設計

線性佈局(LinearLayout)是 Android 中最基礎也最常用的佈局之一,它能讓子元素以垂直或水平的方式排成一列(不設定方向時預設按垂直方向排列)。透過合理的嵌套與 layout_weight,就能輕鬆構建出各種複雜的介面。本文就來詳細解析它的用法。

下面是一個示範 main.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:baselineAligned="false" android:layout_weight="1" > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="@string/color_green" android:textColor="#ff0000" android:background="#00aa00" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="@string/color_blue" android:background="#0000aa" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="@string/color_black" android:background="#000000" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="@string/color_yellow" android:background="#aaaa00" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:text="@string/color_unknown" android:background="#00aaaa" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2"> <TextView android:text="@string/color_red" android:gravity="fill_vertical" android:background="#aa0000" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2"/> <TextView android:text="@string/color_white" android:textColor="#ff0000" android:background="#ffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="2"/> </LinearLayout> </LinearLayout> 

string.xml

 <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, TestAbsoluteLayoutActivity!</string> <string name="app_name">TestAbsoluteLayout</string> <string name= "color_red">red</string> <string name= "color_green">green</string> <string name= "color_blue">blue</string> <string name= "color_white">white</string> <string name= "color_black">black</string> <string name= "color_yellow">yellow</string> <string name= "color_unknown">unknown</string> </resources> 

效果圖如下:

常用的屬性有三個:

android:orientation:設定佈局的方向(vertical 或 horizontal);
android:gravity:用來控制組件的對齊方式;
layout_weight:控制各個組件在佈局中的相對大小比例。

其中 layout_weight 是 LinearLayout 的精髓——它會按權重比例分配剩餘空間,是實現等分、按比例佈局的核心工具。掌握這三個屬性,你就能應付絕大多數線性佈局的需求。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved