*近写了个简单的Android 课程表App,我是个初学者,这个App里使用了:

Android内置的SQLite数据库储存课程数据。
课程的视图用CardView卡片视图。
课程的View是动态加入的,动态添加View的好处是很灵活

如果靠静态的XML构建的话就有点难扩展了,因为你不知道学生一天总共有多少节课

%title插图%num

下面的xml代码是课程表的布局了,一个LinearLayout表示课程表的左侧节数视图,七个Relative表示从星期一到星期天,然后根据用户的输入在正确的地方添加课程视图.代码有点长我没有全部贴出来,整个代码上面还有个父类ScrollView布局的,因为课程表可能有许多节课,是需要下拉的.

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:orientation=”vertical”>

<!–工具条–>
<android.support.v7.widget.Toolbar
android:id=”@+id/toolbar”
android:layout_width=”match_parent”
android:layout_height=”?attr/actionBarSize”
android:background=”#7fab96c5″
app:theme=”@style/ThemeOverlay.AppCompat.Dark.ActionBar”
app:popupTheme=”@style/AlertDialog.AppCompat.Light”/>
<!–周–>
<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”30dp”
android:background=”#7fab96c5″>

<TextView
android:layout_width=”110px”
android:layout_height=”match_parent”
android:gravity=”center”
android:text=”节/周”/>
<TextView
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:gravity=”center”
android:text=”周一”/>
<TextView
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:gravity=”center”
android:text=”周二”/>
<TextView
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:gravity=”center”
android:text=”周三”/>
<TextView
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:gravity=”center”
android:text=”周四”/>
<TextView
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:gravity=”center”
android:text=”周五”/>
<TextView
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:gravity=”center”
android:text=”周六”/>
<TextView
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:gravity=”center”
android:text=”周日”/>
</LinearLayout>
<!–课程表–>
<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”>

<LinearLayout
android:id=”@+id/class_number_layout”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:orientation=”vertical”/>
<RelativeLayout
android:id=”@+id/monday”
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:orientation=”vertical”
android:layout_margin=”1dp”/>
<RelativeLayout
android:id=”@+id/tuesday”
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:orientation=”vertical”
android:layout_margin=”1dp”/>
<RelativeLayout
android:id=”@+id/wednesday”
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:orientation=”vertical”
android:layout_margin=”1dp”/>
<RelativeLayout
android:id=”@+id/thursday”
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:orientation=”vertical”
android:layout_margin=”1dp”/>
<RelativeLayout
android:id=”@+id/friday”
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:orientation=”vertical”
android:layout_margin=”1dp”/>
<RelativeLayout
android:id=”@+id/saturday”
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:orientation=”vertical”
android:layout_margin=”1dp”/>
<RelativeLayout
android:id=”@+id/weekday”
android:layout_width=”0dp”
android:layout_height=”match_parent”
android:layout_weight=”1″
android:orientation=”vertical”
android:layout_marginTop=”1dp”
android:layout_marginLeft=”1dp”
android:layout_marginBottom=”1dp”/>
</LinearLayout>
</LinearLayout>

课程的布局文件
<android.support.v7.widget.CardView
xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
android:layout_width=”45dp”
android:layout_height=”70dp”
app:cardBackgroundColor=”#7feacdd1″
app:cardCornerRadius=”6dp”>

<TextView
android:id=”@+id/text_view”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:layout_gravity=”center”
android:gravity=”center”/>
</android.support.v7.widget.CardView>
这里的宽和高都是预览用的,因为实际的宽和高都是代码控制的.

创造视图的java代码
private void createLeftView(Course course) {
//动态生成课程表左侧的节数视图
int len = course.getEnd();
if (len > maxClassNumber) {
LinearLayout classNumberLayout = (LinearLayout) findViewById(R.id.class_number_layout);
View view;
TextView text;
for (int i = 0; i < len-maxClassNumber; i++) {
view = LayoutInflater.from(this).inflate(R.layout.class_number, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(110,180);
view.setLayoutParams(params);
text = view.findViewById(R.id.class_number_text);
text.setText(“” + number++);
classNumberLayout.addView(view);
}
maxClassNumber = len;
}
}

//创建卡片课程视图
private void createView(final Course course) {
int integer = course.getDay();
if ((integer < 1 && integer > 7) || course.getStart() > course.getEnd()) {
Toast.makeText(this, “星期几没写对,或课程结束时间比开始时间还早~~”, Toast.LENGTH_LONG).show();
} else {
switch (integer) {
case 1: day = (RelativeLayout) findViewById(R.id.monday);break;
case 2: day = (RelativeLayout) findViewById(R.id.tuesday);break;
case 3: day = (RelativeLayout) findViewById(R.id.wednesday);break;
case 4: day = (RelativeLayout) findViewById(R.id.thursday);break;
case 5: day = (RelativeLayout) findViewById(R.id.friday);break;
case 6: day = (RelativeLayout) findViewById(R.id.saturday);break;
case 7: day = (RelativeLayout) findViewById(R.id.weekday);break;
}
final View view = LayoutInflater.from(this).inflate(R.layout.course_card, null); //加载单个课程布局
view.setY(180 * (course.getStart()-1)); //设置开始高度,即第几节课开始
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(ViewGroup.LayoutParams.MATCH_PARENT,(course.getEnd()-course.getStart()+1)*180-2); //设置布局高度,即跨多少节课
view.setLayoutParams(params);
TextView text = view.findViewById(R.id.text_view);
text.setText(course.getCourseName() + “\n” + course.getTeacher() + “\n” + course.getClassRoom()); //显示课程名
day.addView(view);
//长按删除课程
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
view.setVisibility(View.GONE);//先隐藏
day.removeView(view);//再移除课程视图
SQLiteDatabase sqLiteDatabase = databaseHelper.getWritableDatabase();
sqLiteDatabase.execSQL(“delete from course where course_name = ?”, new String[] {course.getCourseName()});
return true;
}
});
}
}

我觉得核心的代码就这些了.