Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android SDK 教學系列:認識 LinearLayout 線性版面

編輯:Android 開發實例

LinearLayout 是 ViewGroup 的一種,它會把內部包含的 View 依線性方式排列,方向可以是垂直,也可以是水平。這是最基礎也最常被使用的版面配置之一。

建立範例專案

新增一個名為 HelloLinearLayout 的專案,然後打開 res/layout/main.xml 填入以下內容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TextView
            android:text="red"
            android:gravity="center_horizontal"
            android:background="#aa0000"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"/>
        <TextView
            android:text="green"
            android:gravity="center_horizontal"
            android:background="#00aa00"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"/>
        <TextView
            android:text="blue"
            android:gravity="center_horizontal"
            android:background="#0000aa"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"/>
        <TextView
            android:text="yellow"
            android:gravity="center_horizontal"
            android:background="#aaaa00"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TextView
            android:text="row one"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <TextView
            android:text="row two"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <TextView
            android:text="row three"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <TextView
            android:text="row four"
            android:textSize="15pt"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    </LinearLayout>

</LinearLayout>

這份 XML 在做什麼

最外層是一個 LinearLayout,orientation 設為 vertical,因此它底下兩個子 LinearLayout 會上下堆疊。

第一個子 LinearLayout 採用 horizontal 排列,裡面放了四個彩色底色的 TextView,因此它們會由左到右並排;第二個子 LinearLayout 採用 vertical 排列,裡面的四個 TextView 便會由上到下排成一列。

兩層的 LinearLayout 都設了 layout_weight="1",所以上下兩塊會平分整個螢幕高度。同理,第一層裡的四個 TextView 也都設了 weight,於是四色方塊會等寬並排。

Java 端

打開 HelloLinearLayout.java,確認它載入了剛才的版面檔:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

setContentView(int) 的作用是載入這個 Activity 的版面檔,而資源 ID R.layout.main 正是指向 res/layout/main.xml。

動手實驗

執行應用後你會看到上半部四個並排的色塊,下半部四行文字。

接著不妨動手改一改:把 android:layout_weight 的值調成不同數字,觀察各個 View 佔用的比例如何變化;也可以試著改 android:orientation,看看排列方向會怎麼轉。版面配置的觀念,靠反覆實驗會比讀文件來得快。

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