Android佈局之LinearLayout(線性佈局)

轉載地址:點擊打開鏈接

線性佈局相對很簡單,也比較容易理解,我們先來看下面這段代碼:

<?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>

這是Android項目中Layout文件下的xml佈局文件,它的效果圖如下

這個例子來自官方文檔,下面對這個佈局進行講解:

android:orientation="vertical"
它確定了LinearLayout的方向,其值可以爲
*vertical, 表示垂直佈局
*horizontal, 表示水平佈局

    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
分別指明瞭在父控件中當前控件的寬和高,可以設定其確定的值,但一般使用下面兩個值
*fill_parent,填滿父控件的空白
*wrap_content,表示大小剛好足夠顯示當前控件裏的內容
    android:gravity="center_horizontal"
如果是沒有子控件的view設置此屬性,表示內容的對齊方式;如果是有子控件的view設置此屬性,則表示子控件的對齊方式(重力傾向),其值如下(需要多個時,用“|”分開)
*top
*bottom
*left
*right
*center_vertical
*center_horizontal
*center
*fill_vertical
*fill_horizontal
*fill
不用具體講解,通過字面意思大家應該能看明白這是什麼意思。

最後給一個稍微複雜的LinearLayout佈局代碼,有興趣可以試一試,後面是效果圖。

<!-- by hoyah 
    14/6/2010 -->
<?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="vertical"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1">
  <TextView
   android:text="red"
   android:gravity="fill_vertical"
   android:background="#aa0000"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"/>
  <TextView
   android:text="white"
   android:textColor="#ff0000"
   android:background="#ffffff"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_weight="1"/>
 </LinearLayout>
 
 <LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  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="green"
    android:textColor="#ff0000"
    android:background="#00aa00"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_weight="1"/>
   <TextView
    android:text="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="black"
    android:background="#000000"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
   <TextView
    android:text="yellow"
    android:background="#aaaa00"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
   <TextView
    android:text="unkown"
    android:background="#00aaaa"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
  </LinearLayout>
  
 </LinearLayout>

</LinearLayout>


效果圖:



發佈了0 篇原創文章 · 獲贊 3 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章