Android动态生成课程表 详解

根据提供的课程信息,动态生成课程表。不同于网上流传的课表形式,课程节数是固定,本课表的课程节数不固定。

1、效果图

Android动态生成课程表 详解_第1张图片Android动态生成课程表 详解_第2张图片

每天共有12节课,上课节数每天都不同。

2、布局文件代码

周一到周日是  7个竖直线性布局文件,其他皆为辅助标题或序号。


    
    
         
         
         
         
         
         
         
         
        
    
	
	    
	        
	     	    
	     	    
	     	    
	     	    
	     	    
	     	    
	     	    
	     	    
	     	    
	     	    
	     	    
	     	    
	     	
	        
	     	
	     	    
	     	
	     	
	     	    
	     	
	     	
	     	    
	     	
	     	
	     	    
	     	
	     	
	     	    
	     	
	     	
	     	    
	     	
	     	
	     	    
	     	
	    
	
3、布局文件中用到的尺寸文件



    40dp
    2dp
    2dp
    
4、颜色文件



    #CCCCCC
    #ECECEC
    #6666CC
    #EDEDED
5、样式文件


    

    
    
    
    
	
    

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(){
    	Listlist1=new ArrayList();
    	Course c1 =new Course("软件工程","A402", 1, 4, "典韦", "1002");
    	list1.add(c1);
    	list1.add(new Course("C语言", "A101", 6, 3, "甘宁", "1001"));
    	courseData[0]=list1;

    	Listlist2=new ArrayList();
    	list2.add(new Course("计算机组成原理", "A106", 6, 3, "马超", "1001"));
    	courseData[1]=list2;
    	
    	Listlist3=new ArrayList();
    	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;
    	
    	Listlist4=new ArrayList();
    	list4.add(new Course("数据结构", "A223", 1, 3, "刘备", "1012"));
    	list4.add(new Course("操作系统", "A405", 6, 3, "曹操", "1014"));
    	courseData[3]=list4;
    	
    	Listlist5=new ArrayList();
    	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,Listdata){
    	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;
    }
    
}






你可能感兴趣的:(android,备忘)