Android中5種布局

來源:互聯網
上載者:User

標籤:

1、LinearLayout

預設orientation是horizontal

注意  android:orientation="horizontal" 和  android:layout_gravity="" 在match_parent 或者 wrap_content 下的關係

layout_gravity操作的是組件的布局  gravity="right" 操作的是組件的內容

 

2、RelativeLayout

子控制項彼此獨立,預設是左上對齊

android:layout_centerHorizontal="true" 

android:layout_centerVertical="true" 

android:layout_centerInParent="true"

android:layout_alignParentLeft="true"

android:layout_toLeftOf="@id/center"

android:layout_below="@id/center"

 

3、FrameLayout

因為沒有方向,所以android:layout_gravity="bottom" 上下左右均可生效

註:在tabhost中必須使用FrameLayout

 

4、TableLayout

TableLayout中android:stretchColumns="1":指定哪列展開填充剩餘空間

TableLayout中列預設是對齊的

TableRow 表示一行,有幾個子節點則代表有幾列

TableLayout 的直系子節點預設layout_width="match_parent" 和 layout_height="wrap_content"且無法修改

TableRow     的直系子節點預設layout_height="wrap_content" 和 layout_width="wrap_content" 且無法修改

layout_column="1" 將當前組件設定為第幾列

layout_span="2" 當前列佔兩列空間

5、AbsoluteLayout 絕對布局

layout_x 和 layout_y 指定空間所在的位置

在針對一種平台或者一種裝置即不需要做螢幕適配時可能會用到

 

demo: 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:stretchColumns="1"    tools:context=".MainActivity" >    <TableRow>        <TextView            android:layout_column="1"            android:text="Open" />        <TextView            android:gravity="right"            android:text="Ctrl-C" />    </TableRow>    <TableRow>        <TextView            android:layout_column="1"            android:text="Save" />        <TextView            android:gravity="right"            android:text="Ctrl." />    </TableRow>    <TableRow>        <TextView            android:layout_column="1"            android:text="Save AS.." />        <TextView            android:gravity="right"            android:text="Ctrl-shift" />    </TableRow>    <TextView        android:layout_height="1dp"        android:background="#000000" />    <TableRow>        <TextView android:text="X" />        <TextView            android:layout_span="2"            android:text="import" />    </TableRow>    <TableRow>        <TextView android:text="X" />        <TextView android:text="Export" />        <TextView            android:gravity="right"            android:text="Ctrl" />    </TableRow>    <TextView        android:layout_height="1dp"        android:background="#000000" />    <TableRow>        <TextView            android:layout_column="1"            android:text="Qut" />    </TableRow></TableLayout>

效果:


   

 

Android中5種布局

聯繫我們

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