android上实现Table

2023-11-12

 

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> mylist = new ArrayList<hashmap>();
                for (int i = 0; i < name.length; i++) {
                        HashMap map1 = new HashMap();
                        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, 79, 45);
                linear.addView(t2, 80, 45);
                linear.addView(t3, 80, 45);
                linear.addView(t4, 80, 45);
                linear.addView(t5, 80, 45);
                
                lv = new ListView(this);
                lv.setHorizontalScrollBarEnabled(true);
                lv.addHeaderView(linear);//
设置listHeader 始终位于列表顶部

                lv.setAdapter(adapter);
        
                HorizontalScrollView hsv = new HorizontalScrollView(this);
                hsv.addView(lv);
                setContentView(hsv);
                
//                lv = (ListView)findViewById(R.layout.main);
//                lv.setHorizontalScrollBarEnabled(true);
//                lv.setSelector(R.drawable.bg_setting_tablerow);
//                lv.setAdapter(adapter);
//                setContentView(lv);
        
                
        }
}

class SpecialAdapter extends SimpleAdapter {
        private int[] colors = new int[] { 0x30FF0000, 0x300000FF };

        public SpecialAdapter(Context context, List> 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;
        }
}
XML
文件
Main.xml


        android:layout_width="fill_parent" android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android" >        
        
         android:layout_height="wrap_content" android:id="@+id/listview">        
                
                android:layout_width="80dip"
                android:layout_height="wrap_content"
                android:textColor="#42AF40"
                android:drawableRight="@drawable/line_y"
                android:id="@+id/a07_ls_txtYF">                
                
                android:layout_width="80dip"
                android:layout_height="wrap_content"
                android:textColor="#42AF40"
                android:drawableRight="@drawable/line_y"
                android:id="@+id/a07_ls_txtXXSR">
                <textview
                android:layout_width="80dip" 
                android:layout_height="wrap_content"
                android:textColor="#42AF40"
            android:drawableRight="@drawable/line_y"
                android:id="@+id/a07_ls_txtYNSE">
                <textview
                android:layout_width="80dip"
                android:layout_height="wrap_content"
                android:textColor="#42AF40"
                android:drawableRight="@drawable/line_y"
                android:id="@+id/a07_ls_txtSNSE">
                <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
</textview
</textview
</hashmap</hashmap>

 

 

效果

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android上实现Table 的相关文章

随机推荐