如何用安卓编写类Excel的表格并填充数据

前段时间应运营需求要求做一个小Demo用来显示数据,在这个过程中,我学会了用代码来做出类似于Excel表格的显示效果,下面就和大家一起分享。


要做成表格形式的布局,很容易让我们想到表格布局,所以先要新建一个layout,在这个layout中我们放入两个TableLayout,其中一个是用来显示表格中的标题,而另一个TableLayout用来显示数据,考虑到可能显示的数据较多,所以外面包上一层ScrollView。主要代码如下:

<TableLayout
        android:id="@+id/tablelayout_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffdedcd2"
        android:stretchColumns="*" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/tablelayout_title"
        android:orientation="vertical">

        <TableLayout
            android:id="@+id/tablelayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ffdedcd2"
            android:stretchColumns="*" />
    </ScrollView>
写好布局文件后,我们开始要填充数据了,也就是我代码中写到的addWidget()方法。数据的话我是造的json类型的假数据,并且新建对象类进行解析,这个就不多说了。先看主要的填充数据的代码。首先是表格的标题,也就是我们通常看到的表格最上方那一栏。主要代码如下:

for (int j = 0; j < 1; j++) {
    TableRow localTableRow1 = new TableRow(this);
    localTableRow1.setBackgroundColor(getResources().getColor(R.color.realtime_table_bg));
    for (int k = 0; k < this.column; k++) {
        TextView localTextView1 = new TextView(this);
        localTextView1.setWidth(this.viewWidth);
        localTextView1.setBackgroundResource(R.drawable.table_shape_title);
        localTextView1.setGravity(17);
        localTextView1.setTextSize(2, 16);
        localTextView1.setTextColor(getResources().getColor(R.color.white));
        localTextView1.getPaint().setFakeBoldText(true);
        localTextView1.setSingleLine();
        switch (k) {
            default:
                break;
            case 0:
                localTextView1.setText("A");
                localTableRow1.addView(localTextView1);
                break;
            case 1:
                localTextView1.setText("B");
                localTableRow1.addView(localTextView1);
                break;
            case 2:
                localTextView1.setText("C");
                localTableRow1.addView(localTextView1);
                break;
            case 3:
                localTextView1.setText("D");
                localTableRow1.addView(localTextView1);
                break;
            case 4:
                localTextView1.setText("E");
                localTableRow1.addView(localTextView1);
                break;
            case 5:
                localTextView1.setText("F");
                localTableRow1.addView(localTextView1);
                break;
            case 6:
                localTextView1.setText("G");
                localTableRow1.addView(localTextView1);
                break;
            case 7:
                localTextView1.setText("H");
                localTableRow1.addView(localTextView1);
        }
    }
    this.tb_title.addView(localTableRow1, new TableLayout.LayoutParams(-1, -2));
}
我们用两个for循环来实现这个数据的填充,最外层for循环之所以判断“j<1”,是因为我们这里只要一行就够了。我们在这里新建一个TabRow,然后再在这一行中添加列。里层的这个for循环中的column就是我们的列数,这里我们用A~H表达我们的列标题名称,通过一个for循环每次新建一个新的TextView,然后判断是第几列,根据位置依次加入我们的列名称。最后在tb_title,也就是我们标题对应的这个TableLayout中添加这些view。

同样的,下面的表格布局也是同样的用两个for循环来实现,只是最外层循环我们是要根据服务器传过来的列表大小来决定了。

因为我们手机通常是书评,可能导致显示不全或不便观看的问题,所以在setContentView()方法之前我加了一个判断,保证一进入这个页面就会显示为横屏:

if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
不过运行之后报错,后来发现是因为这个activity没有设置屏幕显示方向导致的,最后在清单文件里面加上下面这句代码就可以了。
android:screenOrientation="sensorLandscape"
表格布局的实现和运用就介绍到这里了,demo下载地址:http://download.csdn.net/detail/shan286/9475782

猜你喜欢

转载自blog.csdn.net/shan286/article/details/51002798