最近写了个简单的Android 课程表App,我是个初学者,这个App里使用了:
- Android内置的SQLite数据库储存课程数据。
- 课程的视图用CardView卡片视图。
课程的View是动态加入的,动态添加View的好处是很灵活
如果靠静态的XML构建的话就有点难扩展了,因为你不知道学生一天总共有多少节课
下面的xml代码是课程表的布局了,一个LinearLayout表示课程表的左侧节数视图,七个Relative表示从星期一到星期天,然后根据用户的输入在正确的地方添加课程视图.代码有点长我没有全部贴出来,整个代码上面还有个父类ScrollView布局的,因为课程表可能有许多节课,是需要下拉的.
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><!--工具条--><android.support.v7.widget.Toolbarandroid: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"/><!--周--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="30dp"android:background="#7fab96c5"><TextViewandroid:layout_width="110px"android:layout_height="match_parent"android:gravity="center"android:text="节/周"/><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="周一"/><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="周二"/><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="周三"/><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="周四"/><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="周五"/><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="周六"/><TextViewandroid:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:text="周日"/></LinearLayout><!--课程表--><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutandroid:id="@+id/class_number_layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"/><RelativeLayoutandroid:id="@+id/monday"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"android:layout_margin="1dp"/><RelativeLayoutandroid:id="@+id/tuesday"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"android:layout_margin="1dp"/><RelativeLayoutandroid:id="@+id/wednesday"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"android:layout_margin="1dp"/><RelativeLayoutandroid:id="@+id/thursday"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"android:layout_margin="1dp"/><RelativeLayoutandroid:id="@+id/friday"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"android:layout_margin="1dp"/><RelativeLayoutandroid:id="@+id/saturday"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:orientation="vertical"android:layout_margin="1dp"/><RelativeLayoutandroid: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.CardViewxmlns: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"><TextViewandroid: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() {@Overridepublic 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;}});}}
我觉得核心的代码就这些了.
完整的代码在我的github里:https://github.com/izcp/Kcb