Android常用布局範例

來源:互聯網
上載者:User

在Android開發中UI設計十分重要,當使用者使用一個軟體時,最先感受到的不是這款軟體的功能是否強大,而是介面設計是否精緻,使用者體驗是否良好。也可以這樣說,有一個好的介面設計去吸引使用者的使用,才能讓更多的使用者體驗到軟體功能的強大。 下面著重講一下Android中幾種常用布局的使用:

首先,需要說明的是,各個布局既可以單獨使用,也可以嵌套使用,讀者在實際應用中應靈活掌握。

1 LinearLayout 是一種Android中最常用的布局之一,它將自己包含的子項目按照一個方向排列。方向的設定通過Android:orientation=”vertical”(豎直)或者Android:orientation=”horizontal”(水平)來實現。

 

 

代碼如下:

 <LinearLayout android:orientation="vertical" android:layout_width="fill_parent"  android:layout_height="fill_parent">    

    <Button android:id="@+id/bt_1" android:layout_width="fill_parent" 

    android:layout_height="wrap_content"  android:text="用來驗證RelativeLayout"

    />

    <Button android:id="@+id/bt_2" android:layout_width="fill_parent" 

    android:layout_height="wrap_content" android:text="用來驗證TableLayout"

    />

    <Button android:id="@+id/bt_3" android:layout_width="fill_parent" 

    android:layout_height="wrap_content" android:text="用來驗證ListView"

    />  

    <Button android:id="@+id/bt_4" android:layout_width="fill_parent" 

    android:layout_height="wrap_content" android:text="用來驗證FrameLayout"

    />

</LinearLayout>

 

2 FrameLayout對象好比一塊在螢幕上提前預定好的空白地區,可以將一些元素填充在裡面,片。所有元素都被放置在FrameLayout地區的最左上地區,而且無法為這些元素制指定一個確切的位置,若有多個元素,那麼後面的元素會重疊顯示在前一個元素上。

 

代碼如下:

<FrameLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="match_parent" android:layout_height="match_parent">

  <ImageView  android:id="@+id/photo"

       android:src="@drawable/img" android:layout_width="wrap_content"

       android:layout_height="wrap_content"/>

</FrameLayout>

 

3 RelativeLayout是一種相對布局,控制項的位置是按照相對位置來計算的,後一個控制項在什麼位置依賴於前一個控制項的基本位置。是布局最常用,也是最靈活的一種布局。

 

 

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent" android:layout_height="match_parent"

    android:padding="10dip">    

    <TextView android:id="@+id/label" android:layout_width="fill_parent"

        android:layout_height="wrap_content" android:text="請輸入使用者名稱"/>

    <EditText android:id="@+id/entry" android:layout_width="fill_parent"

         android:layout_height="wrap_content" android:layout_below="@id/label"/>

    <Button android:id="@+id/cancel" android:layout_width="wrap_content"

         android:layout_height="wrap_content" android:layout_below="@id/entry"

         android:text="取消"/>     

</RelativeLayout>

 

4 TableLayout

TableLayout是指將子項目的位置分配到行或列中。Android的一個TableLayout有許多TableRow組成,每一個TableRow都會定義一個Row。TableLayout容器不會顯示Row,Column,及Cell的邊框線,每個Row擁有0個或多個Cell,每個Cell擁有一個View對象。

在使用tablelayout時,應注意每一個cell的寬度。

 

 

 

<TableLayout

  xmlns:android="http://schemas.android.com/apk/res/android" 

  android:layout_width="match_parent" android:layout_height="match_parent">  

  <TableRow>

    <TextView android:id="@+id/lable1"  android:text="使用者名稱" android:textStyle="bold"

        android:layout_width="55dip" android:gravity="center"/>

    <EditText android:id="@+id/entry1" android:layout_width="250dip"

        android:layout_height="wrap_content"

        />

  </TableRow>

  <TableRow>

    <TextView android:id="@+id/lable2" android:textStyle="bold" android:text="密碼"

        android:layout_width="55dip" android:gravity="center"/>

    <EditText android:id="@+id/entry2"

        android:layout_width="250dip" android:layout_height="wrap_content"

        android:password="true" android:scrollHorizontally="true"/>

  </TableRow>

</TableLayout>

 

本文出自 “HDDevTeam” 部落格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.