Android自定义View实现课程表表格
GPT4.0+Midjourney绘画+国内大模型 会员永久免费使用!
【 如果你想靠AI翻身,你先需要一个靠谱的工具! 】
自己闲下来时间写的一个课表控件,使用的自定义LinearLayout,里面View都是用代码实现的,最终效果如下图,写的可能有问题希望多多指点
创建一个自定义LinearLayout 控件用来装载课程的信息和课程的周数,和节数大概的布局三这样的
根据上面的看来觉得总体布局我分了两个 上面的星期是一个 下面的节数和格子是一个 总体使用Vertical 而单独内部者使用了Horizontal布局 中间使用了两种布局线条 是这样的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /** * 横的分界线 * * @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
那就从上到下开始先看星期的布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | 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; } } |
布局文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | < 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 > |
最新版查看:查看地址
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
相关文章
android仿微信通讯录搜索示例(匹配拼音,字母,索引位置)
本篇文章主要介绍了android仿微信通讯录搜索示例(匹配拼音,字母,索引位置),具有一定的参考价值,有兴趣的可以了解一下2017-09-09深入解析Android中的setContentView加载布局原理
在日常开发Android中setContentView是必不可少的一部分,下面这篇文章主要给大家介绍了关于Android中setContentView的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习下吧。2017-09-09
最新评论