- Android編程經(jīng)典200例
- 楚無咎編著
- 909字
- 2018-12-30 05:08:24
實(shí)例5 結(jié)構(gòu)緊湊的表格布局
在本小節(jié)中,通過TableLayout表格布局的應(yīng)用,構(gòu)建了一個(gè)顯示學(xué)生信息表的界面,并通過對本程序的具體功能的實(shí)現(xiàn),來向讀者介紹TableLayout表格布局的具體應(yīng)用。
【實(shí)例描述】
本小節(jié)構(gòu)建了一個(gè)顯示學(xué)生信息表小程序,在本程序中可以顯示不同的同學(xué)的一些基本信息。包括學(xué)生的學(xué)號、姓名和籍貫等信息。本實(shí)例的運(yùn)行效果圖,如圖2-5所示。

圖2-5 學(xué)生信息表截圖
提示:在圖2-5中,首先是“學(xué)生信息表”表頭,在表頭下面是字段名稱部分,在字段名稱下面是表中的一些基本信息。。
【實(shí)現(xiàn)過程】
本程序的開發(fā)主要運(yùn)用了TableLayout表格布局的相關(guān)知識。表格布局以行和列的形式管理控件,每行為一個(gè)TableRow對象,也可以是一個(gè)View對象。TableRow可以添加子控件,每次添加一個(gè)子控件即為一列。
【代碼解析】
首先為讀者介紹本程序的主界面main.xml的開發(fā),代碼如下。
代碼位置:見隨書光盤中源代碼/第2章/Sample2_5/ res/layout目錄下的main.xml。
1 <?xml version="1.0" encoding="utf-8"?> 2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" android:layout_height="fill_parent" 4 android:background="#edab4a" 5 android:stretchColumns="0,1,2" android:shrinkColumns="1,2" > 6 <TextView 7 android:text="學(xué)生信息表" 8 android:gravity="center" android:id="@+id/tv1" 9 android:textColor="#FF0000" android:textSize="30dip" 10 android:background="#184124"/> 11 <TableRow> <!--TableRow--> 12 <TextView 13 android:layout_column="0" android:text="學(xué)號" 14 android:gravity="center" android:textColor="#FF0000" 15 android:background="#182241" android:layout_margin="4dip"/> 16 <TextView 17 android:layout_column="1" android:text="姓名" 18 android:gravity="center" android:textColor="#FF0000" 19 android:background="#182241" android:layout_margin="4dip"/> 20 <TextView 21 android:text="籍貫" android:gravity="center" 22 android:textColor="#FF0000" android:background="#182241" 23 android:layout_margin="4dip"/> 24 </TableRow> <!--TableRow--> 25 <TableRow> 26 <TextView 27 android:text=" 20100101 " android:gravity="center" 28 android:textColor="#FF0000" android:background="#182241" 29 android:layout_margin="4dip"/> <!--TextView--> 30 <TextView 31 android:text="張三" android:gravity="left" 32 android:textColor="#FF0000" android:background="#182241" 33 android:layout_margin="4dip"/> 34 <TextView 35 android:text="河北省石家莊市" android:gravity="right" 36 android:textColor="#FF0000" android:background="#182241" 37 android:layout_margin="4dip"/> <!--TextView控件--> 38 </TableRow> <!--TableRow--> 39 <TableRow> 40 <TextView 41 android:text=" 20100102 " android:gravity="center" 42 android:textColor="#FF0000" android:background="#182241" 43 android:layout_margin="4dip"/> 44 <TextView 45 android:text="李四" android:gravity="left" 46 android:textColor="#FF0000" android:background="#182241" 47 android:layout_margin="4dip"/> 48 <TextView 49 android:text="內(nèi)蒙古呼和浩特市" android:gravity="right" 50 android:textColor="#FF0000" android:background="#182241" 51 android:layout_margin="4dip"/> <!--TextView控件--> 52 </TableRow> <!--TableRow--> 53 <TableRow> 54 <TextView 55 android:text="20100103" android:gravity="center" 56 android:textColor="#FF0000" android:background="#182241" 57 android:layout_margin="4dip"/> 58 <TextView 59 android:text="王五" android:gravity="left" 60 android:textColor="#FF0000" android:background="#182241" 61 android:layout_margin="4dip"/> 62 <TextView 63 android:text="廣州" android:gravity="right" 64 android:textColor="#FF0000" android:background="#182241" 65 android:layout_margin="4dip"/> 66 </TableRow> <!--TableRow--> 67 </TableLayout>
提示:在該xml文件中實(shí)現(xiàn)了布局的設(shè)置。總的布局為TableLayout布局。在總布局中,應(yīng)用了4個(gè)TableRow來創(chuàng)建4行表數(shù)據(jù)。并通過對各個(gè)控件的屬性設(shè)置,實(shí)現(xiàn)該小軟件的界面效果。
上面已經(jīng)介紹了本程序主界面main.xml的開發(fā),接下來為讀者介紹本程序的具體功能的實(shí)現(xiàn),代碼如下。
代碼位置:見隨書光盤中源代碼/第2章/Sample2_5/src/com/bn/ex2e目錄下的Sample2_5_Activity.class。
1 package com.bn. ex2e; //包名 2 import android.app.Activity; //包引用聲明 3 import android.os.Bundle; 4 public class Sample2_5_Activity extends Activity { //該類繼承了Activity類 5 public void onCreate(Bundle savedInstanceState){ //實(shí)現(xiàn)onCreate()方法 6 super.onCreate(savedInstanceState); //繼承父類的onCreate()方法 7 setContentView(R.layout.main); //將界面跳轉(zhuǎn)到main.xml界面 8 }}
提示:該類主要實(shí)現(xiàn)main.xml界面的顯示工作。
- BBC監(jiān)聽音箱完全指南
- 通信原理
- 彩色電視機(jī)現(xiàn)場維修實(shí)錄
- 天空地一體化自組織網(wǎng)絡(luò)導(dǎo)航技術(shù)及應(yīng)用
- 邊緣計(jì)算光網(wǎng)絡(luò)
- 通信原理教程
- 通信電子線路
- 電子產(chǎn)品設(shè)計(jì)原理與應(yīng)用
- 室內(nèi)定位理論、方法和應(yīng)用
- 電力通信光纜施工實(shí)訓(xùn)教程
- 微信小程序開發(fā)實(shí)戰(zhàn)
- Cloud-Native Applications in Java
- 光通信波段激光頻率環(huán)的實(shí)現(xiàn)及測速應(yīng)用
- 高速電路設(shè)計(jì)與仿真分析
- LED照明設(shè)計(jì)與應(yīng)用