Android布局(RelativeLayout、TableLayout等)使用方法_Android

來源:互聯網
上載者:User

 本文介紹 Android 介面開發中最基本的四種布局LinearLayout、RelativeLayout、FrameLayout、TableLayout 的使用方法及這四種布局中常用的屬性。

  • LinearLayout 線性布局,布局中空間呈線性排列
  • RelativeLayout 相對布局,通過相對定位的方式,控制控制項位置
  • FrameLayout 幀布局,最簡單的布局,所有控制項放置左上方
  • TableLayout 表格版面配置,以行列方式控制控制項位置

四種布局樣本

1.LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical">   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="150dp"    android:orientation="vertical">     <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="垂直1" />    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="垂直2" />  </LinearLayout>   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="100dp"    android:orientation="horizontal">     <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="水平1" />    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="水平2" />  </LinearLayout>   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="150dp"    android:orientation="horizontal">     <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_gravity="top"      android:text="水平上對齊" />    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_gravity="center_vertical"      android:text="水平垂直置中" />    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_gravity="bottom"      android:text="水平下對齊" />  </LinearLayout>   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="100dp"    android:orientation="horizontal">    <EditText      android:layout_width="0dp"      android:layout_height="wrap_content"      android:layout_weight="3"      android:hint="請輸入..."/>    <Button      android:layout_width="0dp"      android:layout_height="wrap_content"      android:layout_weight="2"      android:text="提交" />  </LinearLayout>   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="100dp"    android:orientation="horizontal">    <EditText      android:layout_width="0dp"      android:layout_height="wrap_content"      android:layout_weight="1"      android:hint="請輸入..."/>    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="提交" />  </LinearLayout></LinearLayout>

orientation:horizontal(水平)/vertical(垂直),表示線性排列的方向。
layout_width/layout_height:元素的寬度與高度
layout_gravity:top/bottom/center/left/right/etc,表示當前元素相對父元素的對齊,多種對齊用“|”隔開,右上對齊:top|right。
layout_weight:佔據空間的比例,例如元素A和B,A設定為1,B設定為3, 元素A、B分別占空間的1/4、3/4,此時元素寬度不由layout_width決定,設定為0dp是比較規範的寫法。
layout_weight 若元素A設定為1,元素B不設定,將layout_width設定為具體的值或wrap_content,那麼元素B的寬度由layout_width決定,元素A將佔滿螢幕剩下的空間。
2.RelativeLayout

<LinearLayout ...>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="300dp">    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentLeft="true"      android:layout_alignParentBottom="true"      android:text="我在左下"/>    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_centerInParent="true"      android:text="我在中間"/>    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_alignParentRight="true"      android:layout_alignParentTop="true"      android:text="我在右上"/>  </RelativeLayout>   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="300dp">    <Button      android:id="@+id/button_2"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_centerInParent="true"      android:text="參照按鈕"/>    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_above="@id/button_2"      android:layout_toRightOf="@id/button_2"      android:text="我在右上"/>    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_below="@id/button_2"      android:layout_toLeftOf="@id/button_2"      android:text="我在左下"/>  </RelativeLayout></LinearLayout>

以下屬性值為true/false

layout_centerHorizontal/layout_centerVertical: 水平置中、垂直置中
layout_centerInparent: 相對父元素垂直&水平置中
layout_alignParentBottom: 元素下邊界和父元素下邊界對齊
layout_alignParentLeft: 左邊界對齊
layout_alignParentRight: 右邊界對齊
layout_alignParentTop: 上邊界對齊
以下屬性值為控制項id

layout_above/layout_below: 在某元素的上方/下方
layout_toLeftOf/layout_toRightOf: 在某元素的左方/右方
layout_alignTop/layout_alignBottom: 元素上(下)邊界與某元素上(下)邊界對齊
layout_alignLeft/layout_alignRight: 左(右)邊界對齊
3.FrameLayout

所有元素都放置在布局的左上方

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent">   <Button    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="我是一個按鈕"/>  <TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="我是一個輸入框"/></FrameLayout>

4.TableLayout

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent">   <TableRow>    <TextView      android:layout_height="wrap_content"      android:text="郵箱"/>    <EditText      android:layout_height="wrap_content"      android:inputType="textEmailAddress"      android:hint="請輸入您的郵箱" />  </TableRow>   <TableRow>    <TextView      android:layout_height="wrap_content"      android:text="密碼"/>    <EditText      android:layout_height="wrap_content"      android:inputType="textPassword"      android:hint="請輸入密碼" />  </TableRow>     <TableRow>    <Button      android:layout_height="wrap_content"      android:layout_span="2"      android:text="註冊" />  </TableRow></TableLayout>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:stretchColumns="1">  ...</TableLayout>

TableRow: 代表表格版面配置的一行,行內一個元素代表一列。
layout_span: 合併儲存格,設定為2,代表該元素佔據2列空間。
stretchColumns: TableRow中無法指定空間寬度,那麼需要用到該屬性,設定為1,表示展開第2列(0為第1列)與螢幕一樣寬,效果如TableLayout的第二張圖。
5.自訂布局

    Android中,布局下可以放置控制項,也可以放置子布局。如果子布局內容較為獨立且經常使用,例如標題列,或者布局比較複雜,這時候可以考慮使用自訂布局的形式匯入。方法很簡單。

建立一個布局檔案,例如example.xml
在父布局中引入:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"   android:layout_width="match_parent"  android:layout_height="match_parent">     <include layout="@layout/example"/> </LinearLayout>

以上就是Android最基本的四種布局的詳細內容介紹,希望對大家的學習有所協助。

聯繫我們

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