第三章 介面UI的基石—UI布局(4),ui基石

來源:互聯網
上載者:User

第三章 介面UI的基石—UI布局(4),ui基石
3.2.5表單布局(TableLayout)

TableLayout,即表單布局,以行和列的形式管理控制項。每行為一個TableRow對象,也可以為一個View對象。當為View對象時,該對象將橫跨該行所有列。

同樣的,我們也以一個簡單的例子來加以說明,看完例子之後相信大家對TableLayout的應用會有一個比較全面的瞭解。

<?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="wrap_content"

    android:stretchColumns="0,1,2"

    android:shrinkColumns="1,2" >

    <TextView

        android:text="TableLayout"

        android:gravity="center"/>

    <TableRow>

        <TextView

            android:layout_column="1"

            android:text="姓名"

            android:gravity="center"/>

        <TextView

            android:text="性別"

            android:gravity="center"/>

    </TableRow>

    <TableRow>

        <TextView

            android:text=" 1 "

            android:gravity="center"/>

        <TextView

            android:text="zhangsan"

            android:gravity="left"/>

        <TextView

            android:text="男"

            android:gravity="center"/>

    </TableRow>

    <TableRow>

        <TextView

            android:text=" 2 "

            android:gravity="center"/>

        <TextView

            android:text="lilei"

            android:gravity="left"/>

        <TextView

            android:text="男"

            android:gravity="center"/>

    </TableRow>

    <TableRow>

        <TextView

            android:text="3"

            android:gravity="center"/>

        <TextView

            android:text="hanmeimei"

            android:gravity="left"/>

        <TextView

            android:text="女"

            android:gravity="center"/>

    </TableRow>

</TableLayout>

 

3-8所示。


圖3-8TableLayout表單布局

 

在TableLayout布局中,有幾個屬性是比較常用的,大家應該熟練掌握(下標都是從0開始):

1)android:stretchColumns="0,1,2",設定1、2、3列為可伸展列,設定完後這些列會將剩餘的空白填滿;

2)android:shrinkColumns="1,2",設定2、3列為可收縮列,設定完後這些列會自動延伸填充可用部分;

3)android:collapse="0",設定第1列隱藏。

3.2.6絕對布局(AbsoluteLayout)

AbsoluteLayout,即絕對布局,又稱座標布局,在布局上靈活性較大,也較複雜。另外由於各種手機螢幕尺寸的差異,給我們的開發也帶來較多困難。

同樣的,我們也舉一個簡單的例子來協助大家掌握。

<?xml version="1.0" encoding="utf-8"?>

<AbsoluteLayout

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

    android:layout_width="wrap_content"

    android:layout_height="wrap_content">

    <ImageView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:src="@drawable/ball"

        android:layout_x="45px"

        android:layout_y="60px"/>

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="absolutelayout"

        android:textColor="@android:color/black"

        android:layout_x="100px"

        android:layout_y="175px"/>

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="absolutelayout"

        android:textColor="@android:color/black"

        android:layout_x="120px"

        android:layout_y="195px"/>

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="absolutelayout"

        android:textColor="@android:color/black"

        android:layout_x="140px"

        android:layout_y="215px"/>

</AbsoluteLayout>

 

3-9所示。


圖3-9AbsoluteLayout絕對布局

 

使用這種布局時,我們要計算好每一個視圖的大小和位置,然後再通過android:layout_x和android:layout_y屬性來將它們的位置定好。

 

經驗分享:

由於現在的Android裝置的螢幕解析度和尺寸有著很大的差異,所以一般情況下,開發過程中已經不會用到絕對布局了,完全可以使用其它幾種布局方式來實現。

如果的確需要使用絕對布局時,有以下幾點需要我們注意:

1)座標原點為螢幕左上方;

2)添加視圖時,要精確的計算每個視圖的像素大小,最好先在紙上畫草圖,並將所有元素的像素定位計算好。

聯繫我們

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