Android动态生成课程表 详解
根据提供的课程信息,动态生成课程表。不同于网上流传的课表形式,课程节数是固定,本课表的课程节数不固定。
1、效果图
每天共有12节课,上课节数每天都不同。
2、布局文件代码
周一到周日是 7个竖直线性布局文件,其他皆为辅助标题或序号。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="20dp" android:gravity="center_horizontal" android:text="课程表" android:textSize="18sp" /> <LinearLayout android:id="@+id/weekName" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="0.5" android:gravity="center_horizontal|center_vertical" android:background="@color/classIndex" android:layout_marginLeft="@dimen/weekItemMarLeft" android:text="" /> <TextView android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" android:gravity="center_horizontal|center_vertical" android:background="@color/classIndex" android:text="周一" /> <TextView android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" android:gravity="center_horizontal|center_vertical" android:background="@color/classIndex" android:text="周二" /> <TextView android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" android:gravity="center_horizontal|center_vertical" android:background="@color/classIndex" android:text="周三" /> <TextView android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" android:gravity="center_horizontal|center_vertical" android:background="@color/classIndex" android:text="周四" /> <TextView android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" android:gravity="center_horizontal|center_vertical" android:background="@color/classIndex" android:text="周五" /> <TextView android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" android:gravity="center_horizontal|center_vertical" android:background="@color/classIndex" android:text="周六" /> <TextView android:layout_width="0dp" android:layout_height="20dp" android:layout_weight="1" android:gravity="center_horizontal|center_vertical" android:background="@color/classIndex" android:text="周日" /> </LinearLayout> <ScrollView android:id="@+id/scrollPanel" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/contentPanel" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:id="@+id/weekPanel_0" 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="@dimen/weekItemHeight" android:gravity="center_horizontal|center_vertical" android:background="@color/classIndex" android:layout_marginTop="@dimen/weekItemMarTop" android:layout_marginLeft="@dimen/weekItemMarLeft" android:text="1" /> <TextView android:layout_width="fill_parent" android:layout_height="@dimen/weekItemHeight" android:gravity="center_horizontal|center_vertical" android:text="2" android:background="@color/classIndex" android:layout_marginTop="@dimen/weekItemMarTop" android:layout_marginLeft="@dimen/weekItemMarLeft" /> <TextView android:layout_width="fill_parent" android:layout_height="@dimen/weekItemHeight" android:gravity="center_horizontal|center_vertical" android:text="3" android:background="@color/classIndex" android:layout_marginTop="@dimen/weekItemMarTop" android:layout_marginLeft="@dimen/weekItemMarLeft" /> <TextView android:layout_width="fill_parent" android:layout_height="@dimen/weekItemHeight" android:gravity="center_horizontal|center_vertical" android:text="4" android:background="@color/classIndex" android:layout_marginTop="@dimen/weekItemMarTop" android:layout_marginLeft="@dimen/weekItemMarLeft" /> <TextView android:layout_width="fill_parent" android:layout_height="@dimen/weekItemHeight" android:gravity="center_horizontal|center_vertical" android:text="5" android:background="@color/classIndex" android:layout_marginTop="@dimen/weekItemMarTop" android:layout_marginLeft="@dimen/weekItemMarLeft" /> <TextView android:layout_width="fill_parent" android:layout_height="@dimen/weekItemHeight" android:gravity="center_horizontal|center_vertical" android:text="6" android:background="@color/classIndex" android:layout_marginTop="@dimen/weekItemMarTop" android:layout_marginLeft="@dimen/weekItemMarLeft" /> <TextView android:layout_width="fill_parent" android:layout_height="@dimen/weekItemHeight" android:gravity="center_horizontal|center_vertical" android:text="7" android:background="@color/classIndex" android:layout_marginTop="@dimen/weekItemMarTop" android:layout_marginLeft="@dimen/weekItemMarLeft" /> <TextView android:layout_width="fill_parent" android:layout_height="@dimen/weekItemHeight" android:gravity="center_horizontal|center_vertical" android:text="8" android:background="@color/classIndex" android:layout_marginTop="@dimen/weekItemMarTop" android:layout_marginLeft="@dimen/weekItemMarLeft" /> <TextView android:layout_width="fill_parent" android:layout_height="@dimen/weekItemHeight" android:gravity="center_horizontal|center_vertical" android:text="9" android:background="@color/classIndex" android:layout_marginTop="@dimen/weekItemMarTop" android:layout_marginLeft="@dimen/weekItemMarLeft" /> <TextView android:layout_width="fill_parent" android:layout_height="@dimen/weekItemHeight" android:gravity="center_horizontal|center_vertical" android:text="10" android:background="@color/classIndex" android:layout_marginTop="@dimen/weekItemMarTop" android:layout_marginLeft="@dimen/weekItemMarLeft" /> <TextView android:layout_width="fill_parent" android:layout_height="@dimen/weekItemHeight" android:gravity="center_horizontal|center_vertical" android:text="11" android:background="@color/classIndex" android:layout_marginTop="@dimen/weekItemMarTop" android:layout_marginLeft="@dimen/weekItemMarLeft" /> <TextView android:layout_width="fill_parent" android:layout_height="@dimen/weekItemHeight" android:gravity="center_horizontal|center_vertical" android:text="12" android:background="@color/classIndex" android:layout_marginTop="@dimen/weekItemMarTop" android:layout_marginLeft="@dimen/weekItemMarLeft" /> </LinearLayout> <!-- 周一到周日的排列 --> <LinearLayout android:id="@+id/weekPanel_1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/weekPanel_2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/weekPanel_3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/weekPanel_4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/weekPanel_5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/weekPanel_6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/weekPanel_7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:orientation="vertical" > </LinearLayout> </LinearLayout> </ScrollView> </LinearLayout>3、布局文件中用到的尺寸文件
<?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="weekItemHeight">40dp</dimen> <dimen name="weekItemMarTop">2dp</dimen> <dimen name="weekItemMarLeft">2dp</dimen> </resources>4、颜色文件
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="classIndex" >#CCCCCC</color> <color name="courseTextColor" >#ECECEC</color> <color name="actionBarBg">#6666CC</color> <color name="actionBarText">#EDEDED</color> </resources>5、样式文件
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="AppBaseTheme" parent="android:Theme.Light"> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <item name="android:actionBarStyle">@style/myActionBarStyle</item> </style> <style name="myActionBarStyle" parent="android:Widget.ActionBar" > <item name="android:background">@color/actionBarBg</item> <item name="android:titleTextStyle">@style/myActionBarTextStyle</item> <item name="android:height">40dp</item> </style> <style name="myActionBarTextStyle"> <item name="android:textSize">18sp</item> <item name="android:textStyle">bold</item> <item name="android:textColor">@color/actionBarText</item> </style> </resources>
6、课程实体类代码
package com.example.testcourse; public class Course { private String name,room,teach,id;//课程名称、上课教室,教师,课程编号 int start,step; //开始上课节次, 一共几节课 public Course(String name, String room, int start, int step, String teach, String id) { super(); this.name = name; this.room = room; this.start = start; this.step = step; this.teach = teach; this.id = id; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getRoom() { return room; } public void setRoom(String room) { this.room = room; } public int getStart() { return start; } public void setStart(int start) { this.start = start; } public int getStep() { return step; } public void setStep(int step) { this.step = step; } public String getTeach() { return teach; } public void setTeach(String teach) { this.teach = teach; } }
7、Activity代码,只要提供7天课程信息,课表可以自动生成。
package com.example.testcourse; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.graphics.Color; import android.text.Layout; import android.util.Log; import android.view.Gravity; import android.view.Menu; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { LinearLayout weekPanels[]=new LinearLayout[7]; List courseData[]=new ArrayList[7]; int itemHeight; int marTop,marLeft; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // itemHeight=getResources().getDimensionPixelSize(R.dimen.weekItemHeight); marTop=getResources().getDimensionPixelSize(R.dimen.weekItemMarTop); marLeft=getResources().getDimensionPixelSize(R.dimen.weekItemMarLeft); //数据 getData(); for (int i = 0; i < weekPanels.length; i++) { weekPanels[i]=(LinearLayout) findViewById(R.id.weekPanel_1+i); initWeekPanel(weekPanels[i], courseData[i]); } } public void getData(){ List<Course>list1=new ArrayList<Course>(); Course c1 =new Course("软件工程","A402", 1, 4, "典韦", "1002"); list1.add(c1); list1.add(new Course("C语言", "A101", 6, 3, "甘宁", "1001")); courseData[0]=list1; List<Course>list2=new ArrayList<Course>(); list2.add(new Course("计算机组成原理", "A106", 6, 3, "马超", "1001")); courseData[1]=list2; List<Course>list3=new ArrayList<Course>(); list3.add(new Course("数据库原理", "A105", 2, 3, "孙权", "1008")); list3.add(new Course("计算机网络", "A405", 6, 2, "司马懿", "1009")); list3.add(new Course("电影赏析", "A112", 9, 2, "诸葛亮", "1039")); courseData[2]=list3; List<Course>list4=new ArrayList<Course>(); list4.add(new Course("数据结构", "A223", 1, 3, "刘备", "1012")); list4.add(new Course("操作系统", "A405", 6, 3, "曹操", "1014")); courseData[3]=list4; List<Course>list5=new ArrayList<Course>(); list5.add(new Course("Android开发","C120",1,4,"黄盖","1250")); list5.add(new Course("游戏设计原理","C120",8,4,"陆逊","1251")); courseData[4]=list5; } public void initWeekPanel(LinearLayout ll,List<Course>data){ if(ll==null || data==null || data.size()<1)return; Log.i("Msg", "初始化面板"); Course pre=data.get(0); for (int i = 0; i < data.size(); i++) { Course c =data.get(i); TextView tv =new TextView(this); LinearLayout.LayoutParams lp =new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT , itemHeight*c.getStep()+marTop*(c.getStep()-1)); if(i>0){ lp.setMargins(marLeft, (c.getStart()-(pre.getStart()+pre.getStep()))*(itemHeight+marTop)+marTop, 0, 0); }else{ lp.setMargins(marLeft, (c.getStart()-1)*(itemHeight+marTop)+marTop, 0, 0); } tv.setLayoutParams(lp); tv.setGravity(Gravity.TOP); tv.setGravity(Gravity.CENTER_HORIZONTAL); tv.setTextSize(12); tv.setTextColor(getResources().getColor(R.color.courseTextColor)); tv.setText(c.getName()+"\n"+c.getRoom()+"\n"+c.getTeach()); //tv.setBackgroundColor(getResources().getColor(R.color.classIndex)); tv.setBackground(getResources().getDrawable(R.drawable.tvshape)); ll.addView(tv); pre=c; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。