Android自动建立表格

来源:互联网 发布:java 工程师要考吗 编辑:程序博客网 时间:2024/06/07 00:38

在开发中我们经常会根据需求需要自动生成一张表格,那么下面就说一下如何做。

布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_main_margin"    android:paddingRight="@dimen/activity_main_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.cheng.myapplication.MainActivity">    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="XX大学学生成绩单"            android:textSize="30sp"            android:textColor="@color/colorPrimaryDark"/>    </LinearLayout>    <TableLayout        android:id="@+id/id_tableLayout"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#000000">        <TableRow            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_margin="0.5dip"            android:background="#000000">            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_margin="1dip"                android:background="#ffffff"                android:layout_weight="1"                android:gravity="center"                android:text="课程"                android:textSize="20dp"                android:textStyle="bold"/>            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_margin="1dip"                android:background="#ffffff"                android:layout_weight="1"                android:gravity="center"                android:text="修习类别"                android:textSize="20dp"                android:textStyle="bold"/>            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_margin="1dip"                android:background="#ffffff"                android:layout_weight="1"                android:gravity="center"                android:text="学分"                android:textSize="20dp"                android:textStyle="bold"/>            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_margin="1dip"                android:background="#ffffff"                android:layout_weight="1"                android:gravity="center"                android:text="成绩"                android:textSize="20dp"                android:textStyle="bold"/>            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_margin="1dip"                android:background="#ffffff"                android:layout_weight="1"                android:gravity="center"                android:text="绩点"                android:textSize="20dp"                android:textStyle="bold"/>        </TableRow>    </TableLayout></LinearLayout>
资源文件/drawable/shape.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android">    <gradient        android:startColor="#ffffff"        android:endColor="#ffffff"        android:angle="0"/>    <stroke        android:width="0.5dp"        android:color="#dedcd2"/>    <corners        android:radius="2dp"/>    <padding        android:left="10dp"        android:top="10dp"        android:right="10dp"        android:bottom="10dp"/></shape>
Java文件MainActivity.Java
package com.example.cheng.myapplication;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.ViewGroup;import android.widget.LinearLayout;import android.widget.TableLayout;import android.widget.TableRow;import android.widget.TextView;public class MainActivity extends Activity {    private final int WC= ViewGroup.LayoutParams.WRAP_CONTENT;    private final int FP= ViewGroup.LayoutParams.WRAP_CONTENT;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        createTable();    }    public void createTable(){        TableLayout tableLayout=(TableLayout)findViewById(R.id.id_tableLayout);        tableLayout.setStretchAllColumns(true);     //设置指定列号的列属性为Stretchable        for(int row=0;row<3;row++){     //产生的行数            TableRow tableRow=new TableRow(MainActivity.this);            tableRow.setBackgroundColor(Color.rgb(222, 220, 210));      //设置表格背景            for (int col = 0; col < 5; col++) {     //产生的列数                TextView tv=new TextView(MainActivity.this);                tv.setBackgroundResource(R.drawable.shape);     //设置背景                tv.setText("测试");                tableRow.addView(tv);            }            tableLayout.addView(tableRow,new LinearLayout.LayoutParams(FP, WC));        }    }}
效果如图

这里写图片描述

0 0
原创粉丝点击