表格类似Excel
只是很简单的实现表格,使用GridView控件——>可以上下左右滚动,但是不能合并
直接上代码:
1、主要布局
<?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">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试表格"/>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<GridView
android:id="@+id/table_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp"
android:numColumns="12">
</GridView>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
2、表格中一个item的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:textSize="15dp"
android:textColor="#000000"></TextView>
</LinearLayout>
3、Adapter代码
package com.example.achartenginetest.adapter;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.achartenginetest.R;
public class TableAdapter extends BaseAdapter {
private final static int NUM_COLUMN = 12;
private LayoutInflater mInflater;
private List<double[]> mOriginalData;
private List<String> mData;
private Context mContext;
public TableAdapter(Context context,List<double[]> mData){
this.mInflater = LayoutInflater.from(context);
this.mOriginalData = mData;
this.mContext = context;
toAllData();
}
private void toAllData(){
mData = new ArrayList<String>();
String[] tableTitle= new String[]{"日期","早餐前","早餐后1小时","早餐后2小时",
"午餐前","午餐后1小时","午餐后2小时",
"晚餐前","晚餐后1小时","晚餐后2小时",
"睡前","凌晨2点"};
for(int i = 0; i<tableTitle.length;++i){
mData.add(tableTitle[i]);
}
for(int i = 0; i<mOriginalData.size();++i){
double[] fixedData = fixData(mOriginalData.get(i));
mData.add(""+i);
for(int j = 0; j<fixedData.length; ++j){
mData.add(""+fixedData[j]);
}
}
}
/**
* 检查数据长度是否为11个取值点
* @param data
* @return 0:11个取值点,1:大于11个取值点,-1:小于11个取值点
*/
private int checkData(double[] data){
return data.length == NUM_COLUMN-1?0:(data.length<NUM_COLUMN-1?-1:1);
}
/**
* 对数据进行修正,保证数据长度的正确性
* @param data
* @return
*/
private double[] fixData(double[] data){
int checkResult = checkData(data);
if(checkResult == 0){
return data;
}else{
double[] result = new double[NUM_COLUMN-1];
if(checkResult == -1){
for(int i =0; i<data.length;++i){
result[i]=data[i];
}
for(int i = data.length; i<NUM_COLUMN-1; ++i){
result[i] = 0;
}
}else{
for(int i =0; i<NUM_COLUMN-1;++i){
result[i]=data[i];
}
}
return result;
}
}
public int getDataCount() {
return mOriginalData.size();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.table_item, null);
holder.vText = (TextView)convertView.findViewById(R.id.text_item);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.vText.setText(mData.get(position));
if(position % NUM_COLUMN == 0){
convertView.setBackgroundColor(Color.WHITE);
}else if(position % NUM_COLUMN == 1 || position % NUM_COLUMN == 2 || position % NUM_COLUMN == 3){
convertView.setBackgroundColor(Color.YELLOW);
}else if(position % NUM_COLUMN == 4 || position % NUM_COLUMN == 5 || position % NUM_COLUMN == 6){
convertView.setBackgroundColor(Color.GREEN);
}else if(position % NUM_COLUMN == 7 || position % NUM_COLUMN == 8 || position % NUM_COLUMN == 9){
convertView.setBackgroundColor(Color.BLUE);
}else if(position % NUM_COLUMN == 10){
convertView.setBackgroundColor(Color.rgb(97,76,32));
}else if(position % NUM_COLUMN == 11){
convertView.setBackgroundColor(Color.GRAY);
}
return convertView;
}
private class ViewHolder{
public TextView vText = null;
}
}
4、Activity代码
package com.example.achartenginetest.charts;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.GridView;
import android.widget.LinearLayout;
import com.example.achartenginetest.R;
import com.example.achartenginetest.adapter.TableAdapter;
public class TableChart extends Activity{
private GridView mGridTable;
private TableAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_table);
mGridTable = (GridView)this.findViewById(R.id.table_grid);
List<double[]> vlist = initList();
mAdapter = new TableAdapter(this,vlist);
mGridTable.setAdapter(mAdapter);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
float density = dm.density;
int gridviewWidth = (int) (90 * 12* density);
int itemWidth = (int) (80 * density);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridviewWidth , LinearLayout.LayoutParams.MATCH_PARENT);
mGridTable.setLayoutParams(params);
mGridTable.setColumnWidth(itemWidth);
mGridTable.setStretchMode(GridView.NO_STRETCH);
mGridTable.setNumColumns(12);
}
private List<double[]> initList(){
List<double[]> vList = new ArrayList<double[]>();
vList.add(new double[]{8.5,11.2,10.3,8.2,15,10.2,8.1,12,9.8,8.6,8.5});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.5,11.2,10.3,8.2,15,10.2,8.1,12,9.8,8.6,8.5});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.5,11.2,10.3,8.2,15,10.2,8.1,12,9.8,8.6,8.5});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
vList.add(new double[]{8.4,10,9.3,8.3,14.1,13,8.0,13,9.6,9.1,8.9});
return vList;
}
}
作者:风倾清凌
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.