在android中实现表格的形式主要有三种:一是Gridview ,实现网状布局,但是它的行和列的大小是定的;二是ListView,这在实际应用中应该用的最多的,它可以通过Layout_weight 属性,实现不同的尺寸列显示,但是当设定Layout_weight 值后,列的尺寸也是固定了的。三是TableLayout,这是文档中专门用于表格显示的布局,通过
android:shrinkColumns
(自动缩进列长度) 和
android:stretchColumns
(自动延伸列长度) 属性可以实现列尺寸的自动缩进和延伸。
下面的例子也是结合了很多参考内容写出来的,在这只想和大家分享,希望能帮到有需要的人。
首先,说说背景吧,最近在做毕设,这个例子也就是其中的一个模块——课程查询。该模块提供两种功能,一个是按学院名查询课程,另一个是按课程名查询课程。并将查询的结构显示在TableLayout布局中,TableLayout的使用可以找些博文,有些讲的非常清楚,在这我就不累赘。先贴一段布局文件的代码,没什么好解释的,都是些简单的布局,所以比较难看。
<?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:background="@color/chat_background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kechengchaxun_anxueyuanchaxun"
android:textSize="18sp" />
<Spinner
android:id="@+id/kccx_spinner1"
android:layout_width="200dip"
android:layout_height="50dp" />
<Button
android:id="@+id/kccx_chaxun1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kechengchaxun_chaxun" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="10.0dip"
android:background="@drawable/fengeview" >
</View>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kechengchaxun_ankechengchaxun"
android:textSize="18sp" />
<EditText
android:id="@+id/kccx_course_name"
android:layout_width="200dip"
android:layout_height="wrap_content"
android:inputType="textCapWords" />
<Button
android:id="@+id/kccx_chaxun2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/kechengchaxun_chaxun" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="20.0dip"
android:background="@drawable/fengeview" >
</View>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent" >
<TableLayout
android:l