首頁 收藏 QQ群
 網(wǎng)站導航

ZNDS智能電視網(wǎng) 推薦當貝市場

TV應用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計算器

綜合交流 / 評測 / 活動區(qū)

交流區(qū) | 測硬件 | 網(wǎng)站活動 | Z幣中心

新手入門 / 進階 / 社區(qū)互助

新手 | 你問我答 | 免費刷機救磚 | ROM固件

查看: 10937|回復: 0
上一主題 下一主題
[教程]

Android基礎教程(二)之五大布局對象

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2013-8-28 16:28 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
>
大家好,我們這一節(jié)講一下Android對用五大布局對象,它們分別是FrameLayout(框架布局:不知道是不是這么翻譯的),LinearLayout (線性布局),AbsoluteLayout(絕對布局),RelativeLayout(相對布局),TableLayout(表格布局).
FrameLayout是最簡單的一個布局對象。它被定制為你屏幕上的一個空白備用區(qū)域,之后你可以在其中填充一個單一對象 — 比如,一張你要發(fā)布的圖片。所有的子元素將會固定在屏幕的左上角;你不能為FrameLayout中的一個子元素指定一個位置。后一個子元素將會直接在前 一個子元素之上進行覆蓋填充,把它們部份或全部擋住(除非后一個子元素是透明的)。
我們看一下效果圖:
  
其中 代碼如下:
   
   
  1. <?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"   
        > <!-- 我們在這里加了一個Button按鈕 -->   
    <Button   
        android:text="button"   
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
    />   
    <TextView   
        android:text="textview"   
        android:textColor="#0000ff"   
        android:layout_width="wrap_content"   
        android:layout_height="wrap_content"   
    />   
    </FrameLayout>
復制代碼
  
   
   
LinearLayout以你為它設置的垂直或水平的屬性值,來排列所有的子元素。所有的子元素都被堆放在其它元素之后,因此一個垂直列表的每一行只會有 一個元素,而不管他們有多寬,而一個水平列表將會只有一個行高(高度為最高子元素的高度加上邊框高度)。LinearLayout保持子元素之間的間隔以 及互相對齊(相對一個元素的右對齊、中間對齊或者左對齊)。
LinearLayout還支持為單獨的子元素指定 。好處就是允許子元素可以填充屏幕上的剩余空間。這也避免了在一個大屏幕中,一串小對象擠 成一堆的情況,而是允許他們放大填充空白。子元素指定一個 值,剩余的空間就會按這些子元素指定的 比例分配給這些子元素。默認的  值為0。例如,如果有三個文本框,其中兩個指定了 值為1,那么,這兩個文本框?qū)⒌缺壤胤糯?,并填滿剩余的空間,而第三個文本框 不會放大。
我們看一下效果圖:
  
其中 l代碼如下:
   
   
  1. <?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="vertical"   
        android:layout_width="fill_parent"   
        android:layout_height="fill_parent"   
        android:layout_weight="2">   
        <TextView   
            android:text="Welcome to Mr Weis blog"   
            android:textSize="15pt"   
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"   
         />   
        </LinearLayout>   
        <LinearLayout   
        android:orientation="horizontal"   
        android:layout_width="fill_parent"   
        android:layout_height="fill_parent"   
        android:layout_weight="1">   
          
        <TextView   
            android:text="red"        android:gravity="center_horizontal" //這里字水平居中   
            android:background="#aa0000"   
            android:layout_width="wrap_content"   
            android:layout_height="fill_parent"   
            android:layout_weight="1"/>   
        <TextView   
            android:text="green"   
            android:gravity="center_horizontal "   
            android:background="#00aa00"   
            android:layout_width="wrap_content"   
            android:layout_height="fill_parent"   
            android:layout_weight="1"/>      
        </LinearLayout>   
    </LinearLayout>
復制代碼
  
   
可以讓子元素指定準確的x/y坐標值,并顯示在屏幕上。(0, 0)為左上角,當向下或向右移動時,坐標值將變大。 沒有頁邊框,允許元素之間互相重疊(盡管不推薦)。我們通常不推薦使用  ,除非你有正當理由要使用它,因為它使界面代碼太過剛性,以至于在不同的設備上可能不能很好地工作。
我們看一下效果圖:
  
其中 l代碼如下:
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"   
        android:orientation="vertical"   
        android:layout_width="fill_parent"   
        android:layout_height="fill_parent"   
        >   
    <EditText   
        android:text="Welcome to Mr Weis blog"   
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
    />   
    <Button   
        android:layout_x="250px" //設置按鈕的X坐標   
        android:layout_y="40px" //設置按鈕的Y坐標   
        android:layout_width="70px" //設置按鈕的寬度   
        android:layout_height="wrap_content"   
        android:text="Button"   
    />   
    </AbsoluteLayout>
復制代碼
      
允許子元素指定他們相對于其它元素或父元素的位置(通過 指定)。因此,你可以以右對齊,或上下,或置于屏幕中央的形式來 排列兩個元素。元素按順序排列,因此如果第一個元素在屏幕的中央,那么相對于這個元素的其它元素將以屏幕中央的相對位置來排列。如果使用 來指定這個 ,在你定義它之前,被關聯(lián)的元素必須定義。
讓我們看一下效果圖:
  
其中 代碼如下:
   
  1. <?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="fill_parent">   
        <TextView   
            android:id="@+id/label"   
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"   
            android:text="Welcome to Mr Weis blog:"/>   
        <EditText   
            android:id="@+id/entry"   
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"   
            android:layout_below="@id/label"/>   
        <Button   
            android:id="@+id/ok"   
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"   
            android:layout_below="@id/entry"   
            android:layout_alignParentRight="true"   
            android:layout_marginLeft="10dip"   
            android:text="OK" />   
        <Button   
            android:layout_width="wrap_content"   
            android:layout_height="wrap_content"   
            android:layout_toLeftOf="@id/ok"   
            android:layout_alignTop="@id/ok"   
            android:text="Cancel" />   
    </RelativeLayout>
復制代碼
   
將子元素的位置分配到行或列中。一個 由許多的 組成,每個 都會定義一個  (事實上,你可以定義其它的子對象,這在下面會解釋到)。 容器不會顯示 、 或 的邊框線。每個  擁有0個或多個的 ;每個 擁有一個 對象。表格由列和行組成許多的單元格。表格允許單元格為空。單元格不能跨列,這與中的不一樣。
下面讓我們看一下效果圖:
  
其中 代碼如下:
   
4K">
  1. <?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:stretchColumns="1">   
        <TableRow>   
            <TextView android:layout_column="1" android:text="Open..." />   
            <TextView android:text="Ctrl-O" android:gravity="right" />   
        </TableRow>   
        <TableRow>   
            <TextView android:layout_column="1" android:text="Save..." />   
            <TextView android:text="Ctrl-S" android:gravity="right" />   
        </TableRow>   
        <View android:layout_height="2dip" android:background="#FF909090" /> //這里是上圖中的分隔線   
        <TableRow>   
            <TextView android:text="X" />   
            <TextView android:text="Export..." />   
            <TextView android:text="Ctrl-E" android:gravity="right " />   
        </TableRow>   
        <View android:layout_height="2dip" android:background="#FF909090" />   
        <TableRow>   
            <TextView android:layout_column="1" android:text="Quit"   
                android:padding="3dip" />   
        </TableRow>   
    </TableLayout>
復制代碼
   以上就是Android五大布局對象,這個工程好龐大,足足花了我一個多小時的時間,希望對大家有幫助~大家多留言!</div

上一篇:Android基礎教程(四)之-----取得手機屏幕大小DisplayMetrics的應用
下一篇:android項目清單介紹
您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

Archiver|新帖|標簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號 )

網(wǎng)絡信息服務信用承諾書 | 增值電信業(yè)務經(jīng)營許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號

GMT+8, 2024-9-20 04:22 , Processed in 0.067816 second(s), 17 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報:report#znds.com (請將#替換為@)

© 2007-2024 ZNDS.Com

快速回復 返回頂部 返回列表