Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android SDK Tutorials系列

Android SDK Tutorials系列

編輯:Android開發實例

  LinearLayout 是 ViewGroup 的一種,裡面包含的View按線性方式排列,要麼垂直方向,要麼水平方向。

  創建一個工程:HelloLinearLayout

  打開 res/layout/main.xml,修改內容如下:
  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://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,它的android:orientation屬性被設置為"vertical",意味著它包含的View(一共兩個)按照垂直方向排列。它包含的第一個View是另一個LinearLayout,這個LinearLayout使用水平排列方式;包含的第二個LinearLayout使用垂直排列方式。兩個內部的LinearLayout都包含了幾個TextView,這些TextView都按照包含他們的LinearLayout規定的方式排列著。

  現在打開HelloLinearLayout.java,確保它裝載了res/layout/main.xml布局文件,修改如下:
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  }
  setContentView(int)
  方法加載這個Activity的布局文件,資源ID — R.layout.main 指向res/layout/main.xml布局文件。

  運行這個應用應該能看到下面的畫面:

  注意XML屬性是如何設置每個View的顯示方式的。試著修改android:layout_weight(layout_width/layout_height)的值,看看各個View是如何顯示的。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved