Android各種Layout特性和使用匯總(二)

來源:互聯網
上載者:User
TableLayout 表格版面配置特性:

類似於HTML中的Table,但顯然不如HTML靈活,可以添加TableRow,然後在TableRow中添加其它的View(如TextView, Button,等),也可以直接在Layout中添加其它的View,類似於LinearLayout。其實TableLayout就是從LinearLayout繼承的。

由於TableLayout其實是個很複雜的Layout,參數較多,需要注意的地方也比較多,所以我找了API Demo中的1個例子(06.More Spanning and Stretchable)進行了修改,先看布局檔案,後面再做詳解。

例子(基於Android API Demo進行的修改):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent" android:layout_height="fill_parent"    android:stretchColumns="1"    android:shrinkColumns="1"    >     <TableRow>        <TextView android:text="Open..."             android:layout_column="1" android:padding="3dip" />        <TextView android:text="Ctrl-O"             android:gravity="right" android:padding="3dip" />    </TableRow>     <TableRow>        <TextView android:text="Save..."            android:layout_column="1" android:padding="3dip" />        <TextView android:text="Ctrl-S"            android:gravity="right" android:padding="3dip" />    </TableRow>     <View        android:layout_height="2dip" android:background="#FF909090" />     <TableRow>        <TextView android:text="X"            android:padding="3dip" />        <TextView android:text="Import..."            android:padding="3dip" />    </TableRow>     <TableRow>        <TextView android:text="X"            android:padding="3dip" />        <TextView android:text="Export..."            android:padding="3dip" />        <TextView android:text="Ctrl-E"            android:gravity="right" android:padding="3dip" />    </TableRow>     <View        android:layout_height="2dip" android:background="#FF909090" />     <TableRow>        <TextView android:text="Quit... And Do A Lot Of Stuff Just To Be Too Long For This Screen Because It Is A Test After All"            android:layout_column="1" android:padding="3dip" />    </TableRow>    <TableRow>        <EditText android:hint="Test Edit" android:layout_span="3"            android:padding="3dip" />    </TableRow></TableLayout>

在一個Activity中調用它,然後運行,效果如:

效果還不錯吧(廢話,這是Android API Demo中的例子!)。好吧,我們逐一講解。

如何放置空白的儲存格?
利用android:layout_column,指定該儲存格的起始位置,則之前的儲存格自動留空,如果希望後面的儲存格留空,則直接不寫即可。例如:

12
        <TextView android:text="Open..."             android:layout_column="1" android:padding="3dip" />

如何靠右對齊?
利用android:gravity指定TextView中的內容靠右對齊,注意不是layout_gravity。
例如:

12
        <TextView android:text="Ctrl-O"             android:gravity="right" android:padding="3dip" />

分割線?分割線!
直接添加1個View在TableLayout中,該View的寬度會自動的fill_parent,指定高度和背景色即可。例如:

12
    <View        android:layout_height="2dip" android:background="#FF909090" />

如何讓1個儲存格跨列(Column Span)?
使用android:layout_span來指定Column Span的列數。例如:

12
        <EditText android:hint="Test Edit" android:layout_span="2"            android:padding="3dip" />

注意:只有在TableRow中的控制項才需要使用layout_span,直接添加在TableLayout中的控制項會自動span所有的列。

如何讓某些列自動擴充?就象LinearLayout中的layout_weight一樣?
初次使用TableLayout的人可能會習慣性的使用layout_weight,希望該View能自動的在TableRow擴充,但正確的做法應該是在TableLayout定義中添加屬性android:stretchColumns,值為需要自動擴充的列的序號。下面是例子。

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

擔心某列內容過多,從而擠壓其它的列,甚至超出範圍?
同上,在TAbleLayout中添加屬性android:shrinkColumns,值為需要自動折行的列的序號。
試著把Layout定義中的android:stretchColumns和android:shrinkColumns去掉,再看看效果?

注意事項:

column的序號是從0開始的。
TableLayout中的元素通常不需要指定寬和高,尤其是寬,TableRow或其它TableLayout的子控制項的寬度都是fill_parent,高度是wrap_content。添加在TableRow中的子控制項的寬和高都是wrap_content。
TableRow中的控制項是自動對齊的,除非你使用了layout_span。
TableLayout中的資料一般是事先準備好的,如果需要動態資料,最好還是使用ListView。

FrameLayout 幀布局特性:

以堆棧方式顯示添加在其中的控制項,最後添加的顯示在最上面。當然,你也可以指定控制項的android:layout_gravity屬性來控制對齊。
看上去不大有用,是嗎?事實上一般來說也確實不大用得到它,不過要知道TabHost控制項就是繼承自FrameLayout的,所以當你想要實現自訂的TabHost控制項時,也許你會想起它。

例子:
12345678910111213141516171819
<?xml version="1.0" encoding="utf-8"?><FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:background="#FFBB0000"android:text="First Text View"android:layout_width="fill_parent" android:layout_height="fill_parent"/><TextView android:background="#FF00BB00"android:text="Second Text View"android:layout_width="wrap_content" android:layout_height="fill_parent"/><TextView android:background="#FF0000BB"android:text="Third Text View"android:layout_width="wrap_content" android:layout_height="wrap_content"android:layout_gravity="right"/></FrameLayout>

運行結果如:

好吧,我承認這個例子挺無聊的,不過它至少展示了FrameLayout的效果,相信你會在特寫的場合找到它的用途的。比如說要做一個可以彈出日曆的日期輸入框?

相關文章

聯繫我們

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