Android布局之LinearLayout(線性布局)

來源:互聯網
上載者:User

Android布局之LinearLayout(線性布局)

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

xml布局檔案

<?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"><br /> <LinearLayout<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:layout_weight="1"><br /> <TextView<br /> android:text="red"<br /> android:gravity="center_horizontal"<br /> android:background="#aa0000"<br /> android:layout_width="wrap_content"<br /> android:layout_height="fill_parent"<br /> android:layout_weight="1"/><br /> <TextView<br /> android:text="green"<br /> android:gravity="center_horizontal"<br /> android:background="#00aa00"<br /> android:layout_width="wrap_content"<br /> android:layout_height="fill_parent"<br /> android:layout_weight="1"/><br /> <TextView<br /> android:text="blue"<br /> android:gravity="center_horizontal"<br /> android:background="#0000aa"<br /> android:layout_width="wrap_content"<br /> android:layout_height="fill_parent"<br /> android:layout_weight="1"/><br /> <TextView<br /> android:text="yellow"<br /> android:gravity="center_horizontal"<br /> android:background="#aaaa00"<br /> android:layout_width="wrap_content"<br /> android:layout_height="fill_parent"<br /> android:layout_weight="1"/><br /> </LinearLayout></p><p> <LinearLayout<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:layout_weight="1"><br /> <TextView<br /> android:text="row one"<br /> android:textSize="15pt"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1"/><br /> <TextView<br /> android:text="row two"<br /> android:textSize="15pt"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1"/><br /> <TextView<br /> android:text="row three"<br /> android:textSize="15pt"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1"/><br /> <TextView<br /> android:text="row four"<br /> android:textSize="15pt"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1"/><br /> </LinearLayout><br /></LinearLayout> 

它的如下

 

 

 

 


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

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布局代碼,有興趣可以試一試,後面是。

<br /><?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> ><br /> <LinearLayout<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1"><br /> <TextView<br /> android:text="red"<br /> android:gravity="fill_vertical"<br /> android:background="#aa0000"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1"/><br /> <TextView<br /> android:text="white"<br /> android:textColor="#ff0000"<br /> android:background="#ffffff"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1"/><br /> </LinearLayout></p><p> <LinearLayout<br /> android:orientation="horizontal"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1"<br /> ><br /> <LinearLayout<br /> android:orientation="horizontal"<br /> android:layout_width="wrap_content"<br /> android:layout_height="fill_parent"<br /> android:layout_weight="1"><br /> <TextView<br /> android:text="green"<br /> android:textColor="#ff0000"<br /> android:background="#00aa00"<br /> android:layout_width="wrap_content"<br /> android:layout_height="fill_parent"<br /> android:layout_weight="1"/><br /> <TextView<br /> android:text="blue"<br /> android:background="#0000aa"<br /> android:layout_width="wrap_content"<br /> android:layout_height="fill_parent"<br /> android:layout_weight="1"/><br /> </LinearLayout></p><p> <LinearLayout<br /> android:orientation="vertical"<br /> android:layout_width="wrap_content"<br /> android:layout_height="fill_parent"<br /> android:layout_weight="1"><br /> <TextView<br /> android:text="black"<br /> android:background="#000000"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1"/><br /> <TextView<br /> android:text="yellow"<br /> android:background="#aaaa00"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1"/><br /> <TextView<br /> android:text="unkown"<br /> android:background="#00aaaa"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content"<br /> android:layout_weight="1"/><br /> </LinearLayout></p><p> </LinearLayout><br /></LinearLayout> 



 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.