Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android動態生成課程表 詳解

Android動態生成課程表 詳解

編輯:關於Android編程

根據提供的課程信息,動態生成課程表。不同於網上流傳的課表形式,課程節數是固定,本課表的課程節數不固定。

1、效果圖

\\

每天共有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()+
+c.getRoom()+
+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;
    }
    
}





 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved