1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| package com.bryan;
import android.app.Activity; import android.content.Context; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast;
public class TableDemoActivity extends Activity { private int colors[] = { Color.rgb(0xee,0xff,0xff), Color.rgb(0xf0,0x96,0x09), Color.rgb(0x8c,0xbf,0x26), Color.rgb(0x00,0xab,0xa9), Color.rgb(0x99,0x6c,0x33), Color.rgb(0x3b,0x92,0xbc), Color.rgb(0xd5,0x4d,0x34), Color.rgb(0xcc,0xcc,0xcc) }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout ll1 = (LinearLayout)findViewById(R.id.ll1); LinearLayout ll2 = (LinearLayout)findViewById(R.id.ll2); LinearLayout ll3 = (LinearLayout)findViewById(R.id.ll3); LinearLayout ll4 = (LinearLayout)findViewById(R.id.ll4); LinearLayout ll5 = (LinearLayout)findViewById(R.id.ll5); LinearLayout ll6 = (LinearLayout)findViewById(R.id.ll6); LinearLayout ll7 = (LinearLayout)findViewById(R.id.ll7); setNoClass(ll1, 2, 0); setClass(ll1, "windows编程实践", "国软 4-503", "1-9周,每一周", "9:50-11:25", 2, 1); setNoClass(ll1, 3, 0); setClass(ll1, "概率论与数理统计", "国软 4-304", "1-15周,每一周", "14:55-17:25", 3, 2); setNoClass(ll1, 2, 0); setClass(ll2, "大学英语", "国软 4-302", "1-18周,每一周", "8:00-9:35", 2, 3); setClass(ll2, "计算机组织体系与结构", "国软 4-204", "1-15,每一周", "9:50-12:15", 3, 5); setNoClass(ll2, 3, 0); setClass(ll2, "团队激励和沟通", "国软 4-204", "1-9周,每一周", "15:45-17:25", 2, 6); setNoClass(ll2, 2, 0); setNoClass(ll3, 2, 0); setClass(ll3, "中国近现代史纲要", "3区 1-328", "1-9周,每一周", "9:50-12:15", 3, 1); setNoClass(ll3, 1, 0); setClass(ll3, "体育(网球)", "信息学部 操场", "6-18周,每一周", "14:00-15:40", 2, 2); setNoClass(ll3, 4, 0); setClass(ll4, "计算机组织体系与结构", "国软 4-204", "1-15,每一周", "8:00-9:35", 2, 5); setClass(ll4, "数据结构与算法", "国软 4-304", "1-18周,每一周", "9:50-12:15", 3, 4); setNoClass(ll4, 1, 0); setClass(ll4, "面向对象程序设计(JAVA)", "国软 1-103", "1-18周,每一周", "14:00-16:30", 3, 5); setNoClass(ll4, 3, 0); setClass(ll5, "c#程序设计", "国软 4-102", "1-9周,每一周", "8:00-9:35", 2, 6); setClass(ll5, "大学英语", "国软 4-302", "1-18周,每一周", "9:50-11:25", 2, 3); setNoClass(ll5, 2, 0); setClass(ll5, "基础物理", "国软 4-304", "1-18周,每一周", "14:00-16:30", 3, 1); setNoClass(ll5, 3, 0); setNoClass(ll6, 12, 0); setNoClass(ll7, 12, 0); }
void setClass(LinearLayout ll, String title, String place, String last, String time, int classes, int color) { View view = LayoutInflater.from(this).inflate(R.layout.item,null); int height=dip2px(this,classes * 50); LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,height); view.setLayoutParams(params); view.setBackgroundColor(colors[color]); ((TextView)view.findViewById(R.id.title)).setText(title); ((TextView)view.findViewById(R.id.place)).setText(place); ((TextView)view.findViewById(R.id.last)).setText(last); ((TextView)view.findViewById(R.id.time)).setText(time); view.setOnClickListener(new OnClickClassListener()); ll.addView(view); }
void setNoClass(LinearLayout ll,int classes, int color) { TextView blank = new TextView(this); if(color == 0) blank.setMinHeight(dip2px(this,classes * 50)); else blank.setBackgroundColor(colors[color]); ll.addView(blank); } class OnClickClassListener implements OnClickListener{ public void onClick(View v) { String title; title = (String) ((TextView)v.findViewById(R.id.title)).getText(); Toast.makeText(getApplicationContext(), "你点击的是:" + title, Toast.LENGTH_SHORT).show(); } } public static int dip2px(Context context, float dpValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f);} public static int px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (int) (pxValue / scale + 0.5f);} }
|