Android自定义View实现课程表表格
程序员文章站
2022-04-29 21:17:17
自己闲下来时间写的一个课表控件,使用的自定义linearlayout,里面view都是用代码实现的,最终效果如下图,写的可能有问题希望多多指点
创建一个自定义line...
自己闲下来时间写的一个课表控件,使用的自定义linearlayout,里面view都是用代码实现的,最终效果如下图,写的可能有问题希望多多指点
创建一个自定义linearlayout 控件用来装载课程的信息和课程的周数,和节数大概的布局三这样的
根据上面的看来觉得总体布局我分了两个 上面的星期是一个 下面的节数和格子是一个 总体使用vertical 而单独内部者使用了horizontal布局 中间使用了两种布局线条 是这样的
/** * 横的分界线 * * @return */ private view getweektransverseline() { textview mweekline = new textview(getcontext()); mweekline.setbackgroundcolor(getresources().getcolor(r.color.view_line)); mweekline.setheight(timetablelineheight); mweekline.setwidth(layoutparams.fill_parent); return mweekline; } /** * 竖向分界线 * * @return */ private view getweekverticalline() { textview mweekline = new textview(getcontext()); mweekline.setbackgroundcolor(getresources().getcolor(r.color.view_line)); mweekline.setheight(dip2px(timetableweeknameheight)); mweekline.setwidth((timetablelineheight)); return mweekline; }
下面就看其它的view
那就从上到下开始先看星期的布局
private void initview() { mhorizontalweeklayout = new linearlayout(getcontext()); mhorizontalweeklayout.setorientation(horizontal); mverticalweeklaout = new linearlayout(getcontext()); mverticalweeklaout.setorientation(horizontal); //表格 for (int i = 0; i <= weeknum; i++) { switch (i) { case 0: //课表出的0,0格子 空白的 textview mtime = new textview(getcontext()); mtime.setheight(dip2px(timetableweeknameheight)); mtime.setwidth((dip2px(timetablenumwidth))); mhorizontalweeklayout.addview(mtime); //绘制1~maxnum linearlayout mmonday = new linearlayout(getcontext()); viewgroup.layoutparams mm = new viewgroup.layoutparams(dip2px(timetablenumwidth), dip2px(maxnum * timetableheight) + maxnum * 2); mmonday.setlayoutparams(mm); mmonday.setorientation(vertical); for (int j = 1; j <= maxnum; j++) { textview mnum = new textview(getcontext()); mnum.setgravity(gravity.center); mnum.settextcolor(getresources().getcolor(r.color.text_color)); mnum.setheight(dip2px(timetableheight)); mnum.setwidth(dip2px(timetablenumwidth)); mnum.settextsize(14); mnum.settext(j + ""); mmonday.addview(mnum); mmonday.addview(getweektransverseline()); } mverticalweeklaout.addview(mmonday); break; case 1: case 2: case 3: case 4: case 5: case 6: case 7: // 设置显示星期一 到星期天 linearlayout mhoriview = new linearlayout(getcontext()); mhoriview.setorientation(vertical); textview mweekname = new textview(getcontext()); mweekname.settextcolor(getresources().getcolor(r.color.text_color)); mweekname.setwidth(((getviewwidth() - dip2px(timetablenumwidth))) / weeknum); mweekname.setheight(dip2px(timetableweeknameheight)); mweekname.setgravity(gravity.center); mweekname.settextsize(16); mweekname.settext(weekname[i - 1]); mhoriview.addview(mweekname); mhorizontalweeklayout.addview(mhoriview); list<timetablemodel> mlistmon = new arraylist<>(); //遍历出星期1~7的课表 for (timetablemodel timetablemodel : mlisttimetable) { if (timetablemodel.getweek() == i) { mlistmon.add(timetablemodel); } } //添加 linearlayout mlayout = gettimetableview(mlistmon, i); mlayout.setorientation(vertical); viewgroup.layoutparams linearparams = new viewgroup.layoutparams((getviewwidth() - dip2px(20)) / weeknum, layoutparams.fill_parent); mlayout.setlayoutparams(linearparams); mlayout.setweightsum(1); mverticalweeklaout.addview(mlayout); break; default: break; } textview l = new textview(getcontext()); l.setheight(dip2px(timetableheight * maxnum) + maxnum * 2); l.setwidth(2); l.setbackgroundcolor(getresources().getcolor(r.color.view_line)); mverticalweeklaout.addview(l); mhorizontalweeklayout.addview(getweekverticalline()); } addview(mhorizontalweeklayout); addview(getweektransverseline()); addview(mverticalweeklaout); addview(getweektransverseline()); }
timetablemodel
package com.shallcheek.timetale; public class timetablemodel { private int id; private int startnum; private int endnum; private int week; private string starttime=""; private string endtime=""; private string name=""; private string teacher=""; private string classroom=""; private string weeknum=""; @override public string tostring() { return "timetablemodel [id=" + id + ", startnum=" + startnum + ", endnum=" + endnum + ", week=" + week + ", starttime=" + starttime + ", endtime=" + endtime + ", name=" + name + ", teacher=" + teacher + ", classroom=" + classroom + ", weeknum=" + weeknum + "]"; } public int getid() { return id; } public int getstartnum() { return startnum; } public int getendnum() { return endnum; } public int getweek() { return week; } public string getstarttime() { return starttime; } public string getendtime() { return endtime; } public string getname() { return name; } public string getteacher() { return teacher; } public string getclassroom() { return classroom; } public string getweeknum() { return weeknum; } public void setid(int id) { this.id = id; } public void setstartnum(int startnum) { this.startnum = startnum; } public void setendnum(int endnum) { this.endnum = endnum; } public void setweek(int week) { this.week = week; } public void setstarttime(string starttime) { this.starttime = starttime; } public void setendtime(string endtime) { this.endtime = endtime; } public void setname(string name) { this.name = name; } public void setteacher(string teacher) { this.teacher = teacher; } public void setclassroom(string classroom) { this.classroom = classroom; } public void setweeknum(string weeknum) { this.weeknum = weeknum; } public timetablemodel() { // todo auto-generated constructor stub } public timetablemodel(int id, int startnum, int endnum, int week, string starttime, string endtime, string name, string teacher, string classroom, string weeknum) { super(); this.id = id; this.startnum = startnum; this.endnum = endnum; this.week = week; this.starttime = starttime; this.endtime = endtime; this.name = name; this.teacher = teacher; this.classroom = classroom; this.weeknum = weeknum; } }
timetableview
package com.shallcheek.timetale; import java.util.arraylist; import java.util.list; import android.content.context; import android.graphics.canvas; import android.util.attributeset; import android.util.log; import android.view.gravity; import android.view.view; import android.view.viewgroup; import android.view.windowmanager; import android.widget.linearlayout; import android.widget.textview; import android.widget.toast; /** * 课表显示view * * @author shallcheek */ public class timetableview extends linearlayout { /** * 配色数组 */ public static int colors[] = {r.drawable.select_label_san, r.drawable.select_label_er, r.drawable.select_label_si, r.drawable.select_label_wu, r.drawable.select_label_liu, r.drawable.select_label_qi, r.drawable.select_label_ba, r.drawable.select_label_jiu, r.drawable.select_label_sss, r.drawable.select_label_se, r.drawable.select_label_yiw, r.drawable.select_label_sy, r.drawable.select_label_yiwu, r.drawable.select_label_yi, r.drawable.select_label_wuw}; private final static int start = 0; //最大节数 public final static int maxnum = 12; //显示到星期几 public final static int weeknum = 7; //单个view高度 private final static int timetableheight = 50; //线的高度 private final static int timetablelineheight = 2; private final static int timetablenumwidth = 20; private final static int timetableweeknameheight = 30; private linearlayout mhorizontalweeklayout;//第一行的星期显示 private linearlayout mverticalweeklaout;//课程格子 private string[] weekname = {"一", "二", "三", "四", "五", "六", "七"}; public static string[] colorstr = new string[20]; int colornum = 0; //数据源 private list<timetablemodel> mlisttimetable = new arraylist<timetablemodel>(); public timetableview(context context) { super(context); } public timetableview(context context, attributeset attributeset) { super(context, attributeset); } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); } /** * 横的分界线 * * @return */ private view getweektransverseline() { textview mweekline = new textview(getcontext()); mweekline.setbackgroundcolor(getresources().getcolor(r.color.view_line)); mweekline.setheight(timetablelineheight); mweekline.setwidth(layoutparams.fill_parent); return mweekline; } /** * 竖向分界线 * * @return */ private view getweekverticalline() { textview mweekline = new textview(getcontext()); mweekline.setbackgroundcolor(getresources().getcolor(r.color.view_line)); mweekline.setheight(dip2px(timetableweeknameheight)); mweekline.setwidth((timetablelineheight)); return mweekline; } private void initview() { mhorizontalweeklayout = new linearlayout(getcontext()); mhorizontalweeklayout.setorientation(horizontal); mverticalweeklaout = new linearlayout(getcontext()); mverticalweeklaout.setorientation(horizontal); //表格 for (int i = 0; i <= weeknum; i++) { switch (i) { case 0: //课表出的0,0格子 空白的 textview mtime = new textview(getcontext()); mtime.setheight(dip2px(timetableweeknameheight)); mtime.setwidth((dip2px(timetablenumwidth))); mhorizontalweeklayout.addview(mtime); //绘制1~maxnum linearlayout mmonday = new linearlayout(getcontext()); viewgroup.layoutparams mm = new viewgroup.layoutparams(dip2px(timetablenumwidth), dip2px(maxnum * timetableheight) + maxnum * 2); mmonday.setlayoutparams(mm); mmonday.setorientation(vertical); for (int j = 1; j <= maxnum; j++) { textview mnum = new textview(getcontext()); mnum.setgravity(gravity.center); mnum.settextcolor(getresources().getcolor(r.color.text_color)); mnum.setheight(dip2px(timetableheight)); mnum.setwidth(dip2px(timetablenumwidth)); mnum.settextsize(14); mnum.settext(j + ""); mmonday.addview(mnum); mmonday.addview(getweektransverseline()); } mverticalweeklaout.addview(mmonday); break; case 1: case 2: case 3: case 4: case 5: case 6: case 7: // 设置显示星期一 到星期天 linearlayout mhoriview = new linearlayout(getcontext()); mhoriview.setorientation(vertical); textview mweekname = new textview(getcontext()); mweekname.settextcolor(getresources().getcolor(r.color.text_color)); mweekname.setwidth(((getviewwidth() - dip2px(timetablenumwidth))) / weeknum); mweekname.setheight(dip2px(timetableweeknameheight)); mweekname.setgravity(gravity.center); mweekname.settextsize(16); mweekname.settext(weekname[i - 1]); mhoriview.addview(mweekname); mhorizontalweeklayout.addview(mhoriview); list<timetablemodel> mlistmon = new arraylist<>(); //遍历出星期1~7的课表 for (timetablemodel timetablemodel : mlisttimetable) { if (timetablemodel.getweek() == i) { mlistmon.add(timetablemodel); } } //添加 linearlayout mlayout = gettimetableview(mlistmon, i); mlayout.setorientation(vertical); viewgroup.layoutparams linearparams = new viewgroup.layoutparams((getviewwidth() - dip2px(20)) / weeknum, layoutparams.fill_parent); mlayout.setlayoutparams(linearparams); mlayout.setweightsum(1); mverticalweeklaout.addview(mlayout); break; default: break; } textview l = new textview(getcontext()); l.setheight(dip2px(timetableheight * maxnum) + maxnum * 2); l.setwidth(2); l.setbackgroundcolor(getresources().getcolor(r.color.view_line)); mverticalweeklaout.addview(l); mhorizontalweeklayout.addview(getweekverticalline()); } addview(mhorizontalweeklayout); addview(getweektransverseline()); addview(mverticalweeklaout); addview(getweektransverseline()); } private int getviewwidth() { windowmanager wm = (windowmanager) getcontext().getsystemservice( context.window_service); return wm.getdefaultdisplay().getwidth(); } private view addstartview(int startnum, final int week, final int start) { linearlayout mstartview = new linearlayout(getcontext()); mstartview.setorientation(vertical); for (int i = 1; i < startnum; i++) { textview mtime = new textview(getcontext()); mtime.setgravity(gravity.center); mtime.setheight(dip2px(timetableheight)); mtime.setwidth(dip2px(timetableheight)); mstartview.addview(mtime); mstartview.addview(getweektransverseline()); final int num = i; //这里可以处理空白处点击添加课表 mtime.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { toast.maketext(getcontext(), "星期" + week + "第" + (start + num) + "节", toast.length_long).show(); } }); } return mstartview; } /** * 星期一到星期天的课表 * * @param model * @param week * @return */ private linearlayout gettimetableview(list<timetablemodel> model, int week) { linearlayout mtimetableview = new linearlayout(getcontext()); mtimetableview.setorientation(vertical); int modesize = model.size(); if (modesize <= 0) { mtimetableview.addview(addstartview(maxnum + 1, week, 0)); } else { for (int i = 0; i < modesize; i++) { if (i == 0) { //添加的0到开始节数的空格 mtimetableview.addview(addstartview(model.get(0).getstartnum(), week, 0)); mtimetableview.addview(getmode(model.get(0))); } else if (model.get(i).getstartnum() - model.get(i - 1).getstartnum() > 0) { //填充 mtimetableview.addview(addstartview(model.get(i).getstartnum() - model.get(i - 1).getendnum(), week, model.get(i - 1).getendnum())); mtimetableview.addview(getmode(model.get(i))); } if (i + 1 == modesize) { mtimetableview.addview(addstartview(maxnum - model.get(i).getendnum(), week, model.get(i).getendnum())); } } } return mtimetableview; } /** * 获取单个课表view 也可以自定义我这个 * * @param model 数据类型 * @return */ @suppresswarnings("deprecation") private view getmode(final timetablemodel model) { linearlayout mtimetableview = new linearlayout(getcontext()); mtimetableview.setorientation(vertical); textview mtimetablenameview = new textview(getcontext()); int num = model.getendnum() - model.getstartnum(); mtimetablenameview.setheight(dip2px((num + 1) * timetableheight) + num * 2); mtimetablenameview.settextcolor(getcontext().getresources().getcolor( android.r.color.white)); mtimetablenameview.setwidth(dip2px(50)); mtimetablenameview.settextsize(16); mtimetablenameview.setgravity(gravity.center); mtimetablenameview.settext(model.getname() + "@" + model.getclassroom()); mtimetableview.addview(mtimetablenameview); mtimetableview.addview(getweektransverseline()); mtimetableview.setbackgrounddrawable(getcontext().getresources() .getdrawable(colors[getcolornum(model.getname())])); mtimetableview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { toast.maketext(getcontext(), model.getname() + "@" + model.getclassroom(), toast.length_long).show(); } }); return mtimetableview; } /** * 转换dp * * @param dpvalue * @return */ public int dip2px(float dpvalue) { float scale = getcontext().getresources().getdisplaymetrics().density; return (int) (dpvalue * scale + 0.5f); } public void settimetable(list<timetablemodel> mlist) { this.mlisttimetable = mlist; for (timetablemodel timetablemodel : mlist) { addtimename(timetablemodel.getname()); } initview(); invalidate(); } /** * 输入课表名循环判断是否数组存在该课表 如果存在输出true并退出循环 如果不存在则存入colorst[20]数组 * * @param name */ private void addtimename(string name) { boolean isrepeat = true; for (int i = 0; i < 20; i++) { if (name.equals(colorstr[i])) { isrepeat = true; break; } else { isrepeat = false; } } if (!isrepeat) { colorstr[colornum] = name; colornum++; } } /** * 获取数组中的课程名 * * @param name * @return */ public static int getcolornum(string name) { int num = 0; for (int i = 0; i < 20; i++) { if (name.equals(colorstr[i])) { num = i; } } return num; } }
布局文件
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white" android:orientation="vertical" > <scrollview android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.shallcheek.timetale.timetableview android:id="@+id/main_timetable_ly" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > </com.shallcheek.timetale.timetableview> </scrollview> </linearlayout>
最新版查看:查看地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。