Android零基礎入門第29節:善用TableLayout表格版面配置,事半功倍

來源:互聯網
上載者:User

標籤:概述   nbsp   系統架構   比較   www   完全   頁面   類比   content   

原文:Android零基礎入門第29節:善用TableLayout表格版面配置,事半功倍

前面學習了線性布局和相對布局,線性布局雖然方便,但如果遇到控制項需要排列整齊的情況就很難達到要求,用相對布局又比較麻煩,為此Android系統中提供了表格版面配置。

 

 

一、認識TableLayout

表格版面配置就是讓控制項以表格的形式來排列控制項,只要將控制項放在儲存格中,控制項就可以整齊地排列,使用TableLayout標籤。

TableLayout繼承了 LinearLayout,因此它的本質依然是線性布局管理器。每次向TableLayout中添加一個TableRow,該TableRow就是一個表格行,TableRow也是容器,因此它也可以不斷地添加其他組件,每添加一個子組件該表格就增加一列。如果直接向TableLayout中添加組件,那麼這個組件將直接佔用一行。

在表格版面配置中,列的寬度由該列中最寬的那個儲存格決定,整個表格版面配置的寬度則取決於父容器的寬度(預設總是佔滿父容器本身)。

在表格版面配置管理器中,可以為儲存格設定如下3種行為方式。

  • Shrinkable:如果某個列被設為Shrinkable,那麼該列的所有儲存格的寬度可以被收縮,以保證該表格能適應父容器的寬度。

  • Stretchable:如果某個列被設為Stretchable,那麼該列的所有儲存格的寬度可以被展開,以保證組件能完全填滿表格空餘空間。

  • Collapsed:如果某個列被設為Collapsed,那麼該列的所有儲存格會被隱藏。

TableLayout繼承了 LinearLayout,因此它完全可以支援LinearLayout所支援的全部XML屬性。除此之外,TableLayout還支援如下表所示的XML屬性和相關方法。

 

二、樣本

接下來通過一個簡單的樣本程式來學習TableLayout的使用用法。

同樣使用WidgetSample工程,繼續使用app/main/res/layout/目錄下的activity_main.xml檔案,在其中填充如下程式碼片段:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_width="match_parent"              android:layout_height="match_parent"              android:orientation="vertical">    <!-- 定義第一個表格版面配置,指定第2列允許收縮,第3列允許展開 -->    <TableLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:shrinkColumns="1"        android:stretchColumns="2">        <!-- 直接添加按鈕,它自己會佔一行 -->        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="獨自一行的按鈕"/>        <!-- 添加一個表格行 -->        <TableRow>            <!-- 為該表格行添加三個按鈕 -->            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通按鈕"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="收縮的按鈕"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="展開的按鈕"/>        </TableRow>    </TableLayout>    <!-- 定義第2個表格版面配置 ,指定第2列隱藏-->    <TableLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:collapseColumns="1">        <!-- 添加一個表格行 -->        <TableRow>            <!-- 為該表格行添加三個按鈕 -->            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通按鈕1"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通按鈕2"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通按鈕3"/>        </TableRow>    </TableLayout>    <!-- 定義第3個表格版面配置,指定第2列和第3列可以被展開-->    <TableLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:stretchColumns="1,2">        <!--定義一個表格行-->        <TableRow>            <!-- 為該表格行添加三個按鈕 -->            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通按鈕" />            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="展開的按鈕" />            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="展開的按鈕" />        </TableRow>        <!--定義一個表格行-->        <TableRow>            <!-- 為該表格行添加兩個按鈕 -->            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="普通按鈕" />            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="展開的按鈕" />        </TableRow>    </TableLayout></LinearLayout>

上面頁面中定義了 3個TableLayout,3個TableLayout中粗體字代碼指定了它們對各列的控制行為。

  • 第1個TableLayout,指定第2列允許收縮,第3列允許展開。

  • 第2個TableLayout,指定第2列被隱藏。

  • 第3個TableLayout,指定第2列和第3列允許展開。

運行程式,可以看到所示介面效果。

需要注意的是TableRow不需要設定寬度layout_width和高度layoutJieight,其寬度一定是match_parent,即自動填滿父容器,高度一定為wrap_content,即根據內容改變高度。但對於TableRow中的其他控制項來說,是可以設定寬度和高度的,但必其須是 wrap_content 或者 fill_parent。

到此,TableLayout的樣本結束,關於TableLayout的更多用法可以多動手練習。

 

 

今天就先到這裡,如果有問題歡迎留言一起探討,也歡迎加入Android零基礎入門技術討論群,共同成長!

此文章著作權為公眾號分享達人秀(ShareExpert)——鑫鱻所有,若轉載請備忘出處,特此聲明!

 

往期總結分享:

Android零基礎入門第1節:Android的前世今生

Android零基礎入門第2節:Android 系統架構和應用組件那些事

Android零基礎入門第3節:帶你一起來聊一聊Android開發環境

Android零基礎入門第4節:正確安裝和配置JDK, 高富帥養成第一招

Android零基礎入門第5節:善用ADT Bundle, 輕鬆邂逅女神

Android零基礎入門第6節:配置最佳化SDK Manager, 正式約會女神

Android零基礎入門第7節:搞定Android模擬器,開啟甜蜜之旅

Android零基礎入門第8節:HelloWorld,我的第一趟旅程出發點

Android零基礎入門第9節:Android應用實戰,不懂代碼也可以開發

Android零基礎入門第10節:開發IDE大升級,終於迎來了Android Studio

Android零基礎入門第11節:簡單幾步帶你飛,運行Android Studio工程

Android零基礎入門第12節:熟悉Android Studio介面,開始裝逼賣萌

Android零基礎入門第13節:Android Studio配置最佳化,打造開發利器

Android零基礎入門第14節:使用高速Genymotion,跨入火箭時代

Android零基礎入門第15節:掌握Android Studio項目結構,揚帆起航

Android零基礎入門第16節:Android使用者介面開發概述

Android零基礎入門第17節:TextView屬性和方法大全

Android零基礎入門第18節:EditText的屬性和使用方法

Android零基礎入門第19節:Button使用詳解

Android零基礎入門第20節:CheckBox和RadioButton使用大全

Android零基礎入門第21節:ToggleButton和Switch使用大全

Android零基礎入門第22節:ImageView的屬性和方法大全

Android零基礎入門第23節:ImageButton和ZoomButton使用大全

Android零基礎入門第24節:自訂View簡單使用,打造屬於你的控制項

Android零基礎入門第25節:簡單且最常用的LinearLayout線性布局

Android零基礎入門第26節:兩種對齊,layout_gravity和gravity大不同

Android零基礎入門第27節:正確使用padding和margin

Android零基礎入門第28節:輕鬆掌握RelativeLayout相對布局

Android零基礎入門第29節:善用TableLayout表格版面配置,事半功倍

相關文章

聯繫我們

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