步步為營_Android開發課[14]_使用者介面之Layout(布局),_androidlayout

來源:互聯網
上載者:User

步步為營_Android開發課[14]_使用者介面之Layout(布局),_androidlayout

Focus on technology, enjoy life!—— QQ:804212028
瀏覽連結:http://blog.csdn.net/y18334702058/article/details/44624305

  • 主題:使用者介面之Layout(布局)
    -在Android開發中我們有傳說中的5大布局,它們的結合使用,畫出了APP介面的條條框框。

Android中常用的5大布局:
線性布局(LinearLayout):按照垂直或者水平方向布局的組件。
幀布局(FrameLayout):組件從螢幕左上方布局組件。
表格版面配置(TableLayout):按照行列方式布局組件。
相對布局(RelativeLayout):相對其它組件的布局方式。
絕對布局(AbsoluteLayout):按照絕對座標來布局組件。

各種布局之間的關係圖:

1.線性布局(LinearLayout):

線性布局是Android開發中最常見的一種布局方式,它是按照垂直或者水平方向來布局,通過“android:orientation”屬性可以設定線性布局的方向。屬性值有垂直(vertical)和水平(horizontal)兩種。

main.xml原始碼:

<?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="wrap_content"                >        <TextView             android:layout_height="wrap_content"            android:layout_width="fill_parent"            android:background="#005588"            android:layout_weight="1"            />        <TextView             android:layout_height="wrap_content"            android:layout_width="fill_parent"            android:background="#337799"            android:layout_weight="1"            />          <TextView             android:layout_height="wrap_content"            android:layout_width="fill_parent"            android:background="#227508"            android:layout_weight="1"            />        <TextView             android:layout_height="wrap_content"            android:layout_width="fill_parent"            android:background="#545729"            android:layout_weight="1"            />      </LinearLayout>    <LinearLayout         android:orientation="vertical"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        >        <TextView             android:layout_height="wrap_content"            android:layout_width="fill_parent"            android:background="#234235"            />        <TextView             android:layout_height="wrap_content"            android:layout_width="fill_parent"            android:background="#865321"            />      </LinearLayout></LinearLayout>

運行結果:

2.幀布局(FrameLayout):

幀布局是從螢幕的左上方(0,0)座標開始布局,多個組件層疊排列,第一個添加的組件放到最底層,最後添加到架構中的視圖顯示在最上面。上一層的會覆蓋下一層的控制項。

mian.xml原始碼:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     >     <TextView            android:layout_width="300dp"           android:layout_height="300dp"           android:background="#114488"                  />     <TextView            android:layout_width="250dp"           android:layout_height="250dp"           android:background="#225599"                  />     <TextView            android:layout_width="200dp"           android:layout_height="200dp"           android:background="#337700"                  /> </FrameLayout> 

運行結果:

3.表格版面配置(TableLayout):

表格版面配置是一個ViewGroup以表格顯示它的子視圖(view)元素,即行和列標識一個視圖的位置。
表格版面配置常用的屬性如下:
android:collapseColumns:隱藏指定的列
android:shrinkColumns:收縮指定的列以適合螢幕,不會擠出螢幕
android:stretchColumns:盡量把指定的列填充空白部分
android:layout_column:控制項放在指定的列
android:layout_span:該控制項所跨越的列數

mian.xml原始碼:

<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:shrinkColumns="0,1,2"    >     <TableRow>         <Button             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Button1"             android:layout_column="0"            />         <Button             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Button2"             android:layout_column="1"            />     </TableRow>     <TableRow>        <Button             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Button3"             android:layout_column="1"            />        <Button             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Button4"             android:layout_column="2"            />     </TableRow> </TableLayout> 

運行結果:

4.相對布局(RelativeLayout):

相對布局是按照組件之間的相對位置來布局,比如相對於某個組件的上,下,左,右或者相對於父布局的上,下,左,右。

mian.xml原始碼:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:padding="10px"     >     <TextView            android:id="@+id/tev1"         android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:layout_marginBottom="30dp"         android:text="嘟嘟跑哪裡去了:"         />     <EditText         android:id="@+id/tx1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_below="@id/tev1"         />     <Button         android:id="@+id/btn1"         android:layout_height="wrap_content"         android:layout_width="wrap_content"         android:layout_below="@id/tx1"         android:layout_alignParentRight="true"         android:text="確定"         />     <Button         android:id="@+id/btn2"         android:layout_height="wrap_content"         android:layout_width="wrap_content"         android:layout_below="@id/tx1"         android:layout_toLeftOf="@id/btn1"         android:layout_marginRight="30dp"         android:text="取消"         /> </RelativeLayout> 

運行結果:

絕對布局(AbsoluteLayout):

絕對布局不能滿足適配各種不同尺寸的機型,逐漸地被淘汰,已經沒人使用了喲!大家不用學了喲,好高興^_^!

Focus on technology, enjoy life!—— QQ:804212028
瀏覽連結:http://blog.csdn.net/y18334702058/article/details/44624305

聯繫我們

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