课程表的实现

主要是使用ScrollView和LinearLayout来实现,比较简单

下面是效果图

activity布局

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?xml version="1.0" encoding="utf-8"?>
<!-- 定义ScrollView,为里面的组件添加垂直滚动条 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:gravity="center"
android:text="7月"
android:textColor="#ffffff"
android:textSize="10sp" />

<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期一"
android:textColor="#ffffff"
android:textSize="10sp" />

<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期二"
android:textColor="#ffffff"
android:textSize="10sp" />

<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期三"
android:textColor="#ffffff"
android:textSize="10sp" />

<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期四"
android:textColor="#ffffff"
android:textSize="10sp" />

<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期五"
android:textColor="#ffffff"
android:textSize="10sp" />

<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期六"
android:textColor="#ffffff"
android:textSize="10sp" />

<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期日"
android:textColor="#ffffff"
android:textSize="10sp" />
</LinearLayout>

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadingEdge="none"
android:scrollbars="none"
android:overScrollMode="never" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="1"
android:textColor="#666666"
android:textSize="12sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="2"
android:textColor="#666666"
android:textSize="12sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="3"
android:textColor="#666666"
android:textSize="12sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="4"
android:textColor="#666666"
android:textSize="12sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="5"
android:textColor="#666666"
android:textSize="12sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="6"
android:textColor="#666666"
android:textSize="12sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="7"
android:textColor="#666666"
android:textSize="12sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="8"
android:textColor="#666666"
android:textSize="12sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="9"
android:textColor="#666666"
android:textSize="12sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="10"
android:textColor="#666666"
android:textSize="12sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="11"
android:textColor="#666666"
android:textSize="12sp" />

<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="12"
android:textColor="#666666"
android:textSize="12sp" />
</LinearLayout>

<LinearLayout
android:id="@+id/ll1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />

<LinearLayout
android:id="@+id/ll2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />

<LinearLayout
android:id="@+id/ll3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />

<LinearLayout
android:id="@+id/ll4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />

<LinearLayout
android:id="@+id/ll5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />

<LinearLayout
android:id="@+id/ll6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />

<LinearLayout
android:id="@+id/ll7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
</LinearLayout>
</ScrollView>

</LinearLayout>

课程项布局

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
<?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:gravity="center"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="12sp"/>
<TextView
android:id="@+id/place"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="10sp"/>
<TextView
android:id="@+id/last"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:textColor="#ffffff"
android:textSize="10sp"/>
<TextView
android:id="@+id/time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="13dp"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#ffffff"
android:textSize="10sp"/>
</LinearLayout>

逻辑实现

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)
};

/** Called when the activity is first created. */
@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);

}
/**
* 设置课程的方法
* @param ll
* @param title 课程名称
* @param place 地点
* @param last 时间
* @param time 周次
* @param classes 节数
* @param color 背景色
*/
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); //48
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设置点击的监听器
view.setOnClickListener(new OnClickClassListener());
//上下添加部分间隙
// TextView blank1 = new TextView(this);
// TextView blank2 = new TextView(this);
// blank1.setHeight(dip2px(this,classes));
// blank2.setHeight(dip2px(this,classes));
// ll.addView(blank1);
ll.addView(view);
// ll.addView(blank2);
}
/**
* 设置无课(空百)
* @param ll
* @param classes 无课的节数(长度)
* @param color
*/
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) {
// TODO Auto-generated method stub
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);} /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);}


}

主要是设置有课的数量和没课的数量