1、activity代码
package com.android;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class TableList extends Activity {
private ListView lv;
/** Called when the activity is first created. */
String name[]={"周旋","于世龙","于翠","杨涛","覃达","周旋","于世龙","于翠","杨涛","覃达"};
String course[]={"语文","数学","英语","历史","政治","语文","数学","英语","历史","政治"};
String score[]={"100","90","30","50","90","100","90","30","50","90"};
String id[]={"10123","10124","10125","10126","10127","10123","10124","10125","10126","10127"};
String py[]={"良好","良好","一般","良好","良好","良好","很好","差","一般","不好"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < name.length; i++) {
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("Name", name[i]);
map1.put("ID", id[i]);
map1.put("Course", course[i]);
map1.put("Score", score[i]);
map1.put("PY", py[i]);
mylist.add(map1);
}
SpecialAdapter adapter = new SpecialAdapter(this, mylist,
R.layout.main, new String[] {"PY", "Course", "Score",
"ID", "Name" }, new int[] {R.id.a07_ls_txtSNSRE, R.id.a07_ls_txtYNSE, R.id.a07_ls_txtSNSE,
R.id.a07_ls_txtXXSR, R.id.a07_ls_txtYF });
TextView t1 = new TextView(this);
t1.setText("姓名");
t1.setBackgroundResource(R.drawable.bk);
TextView t2 = new TextView(this);
t2.setText("学号");
t2.setBackgroundResource(R.drawable.bk);
TextView t3 = new TextView(this);
t3.setText("科目");
t3.setBackgroundResource(R.drawable.bk);
TextView t4 = new TextView(this);
t4.setText("成绩");
t4.setBackgroundResource(R.drawable.bk);
TextView t5 = new TextView(this);
t5.setText("总评");
t5.setBackgroundResource(R.drawable.bk);
LinearLayout linear = new LinearLayout(this);
linear.setBackgroundResource(R.drawable.bg_comment_grey);
linear.addView(t1, 40, 35);
linear.addView(t2, 45, 35);
linear.addView(t3, 45, 35);
linear.addView(t4, 53, 35);
linear.addView(t5, 55, 35);
lv = new ListView(this);
lv.setHorizontalScrollBarEnabled(true);
lv.addHeaderView(linear);// 设置listHeader 始终位于列表顶部
lv.setAdapter(adapter);
HorizontalScrollView hsv = new HorizontalScrollView(this);
hsv.addView(lv);
setContentView(hsv);
}
}
class SpecialAdapter extends SimpleAdapter {
private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
public SpecialAdapter(Context context, List<? extends Map<String, ?>> data,
int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
if (colorPos == 1) {
view.setBackgroundColor(Color.argb(250, 255, 255, 255));
} else {
view.setBackgroundColor(Color.argb(250, 224, 243, 250));
}
return view;
}
}
2、XMl文件
<TableLayout android:id="@+id/tableLayout1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TableRow android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="@+id/listview">
<TextView
android:layout_width="55dip"
android:layout_height="wrap_content"
android:textColor="#42AF40"
android:drawableRight="@drawable/line_y"
android:id="@+id/a07_ls_txtYF"></TextView>
<TextView
android:layout_width="60dip"
android:layout_height="wrap_content"
android:textColor="#42AF40"
android:drawableRight="@drawable/line_y"
android:id="@+id/a07_ls_txtXXSR"></TextView>
<TextView
android:layout_width="60dip"
android:layout_height="wrap_content"
android:textColor="#42AF40"
android:drawableRight="@drawable/line_y"
android:id="@+id/a07_ls_txtYNSE"></TextView>
<TextView
android:layout_width="70dip"
android:layout_height="wrap_content"
android:textColor="#42AF40"
android:drawableRight="@drawable/line_y"
android:id="@+id/a07_ls_txtSNSE"></TextView>
<TextView
android:layout_width="80dip"
android:layout_height="wrap_content"
android:textColor="#42AF40"
android:drawableRight="@drawable/line_y"
android:id="@+id/a07_ls_txtSNSRE"></TextView>
</TableRow>
</TableLayout>