Android开发 * 课程表的布局实例详解
作者:起风的早晨 发布时间:2022-02-01 18:33:19
先说下这个demo,这是一个模仿课程表的布局文件,虽然我是个菜鸟,但我还是想留给学习的人一些例子,先看下效果
然后再来看一下我们学校的app
布局分析
先上一张划分好了的布局图
首先整个页面放在一个LinearLayout布局下面,分为上面和下面两个部分,下面一个是显示课程表的详细信息
1:这个没什么好讲的,就是直接一个LinearLayout布局,然后将控件一个TextView用来显示年份,一个View用来当作竖线,一个Spinner用来显示选择周数
2:这个是显示星期几的部件,是我自定义的View,自己重写onDraw方法,然后画出七个字,代码如下:
public void onDraw(Canvas canvas)
{
//获得当前View的宽度
int width = getWidth();
int offset = width / 8;
int currentPosition = offset;
//设置要绘制的字体
mPaint.setTypeface(Typeface.create(Typeface.DEFAULT_BOLD, Typeface.BOLD));
mPaint.setTextSize(30);
mPaint.setColor(Color.rgb(0, 134, 139));
for(int i = 0; i < 7 ; i++)
{
//圈出当前的日期
if( day == i)
{
System.out.println("画出当前的日期!");
}
canvas.drawText(days[i], currentPosition, 30, mPaint);
currentPosition += offset;
}
//调用父类的绘图方法
super.onDraw(canvas);
}
3:这个也是一个LinearLayout,直接手写布局,将写出来的,将LinearLayout的orientation 属性设置为vertical 。
4:这是一个GridView ,然后自己继承BaseAdapter 填充内容,下面放上布局的xml文件
<?xml version="1.0" encoding="utf-8"?>
<!--模仿课程表的界面-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--显示时间-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginLeft="20dp"
android:textSize="20dp"
android:text="2016年"/>
<!--竖线-->
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="#00FFFF"
/>
<!--下拉方式选周数-->
<Spinner
android:id="@+id/switchWeek"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
/>
</LinearLayout>
<!--分隔线-->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#00FF7F"/>
<!--显示星期-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@android:color/white">
<cn.karent.demo.UI.WeekTitle
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginTop="10dp"/>
</LinearLayout>
<!--显示课表详细信息-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--显示多少节课-->
<LinearLayout
android:layout_width="25dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="92dp"
android:text="一"
android:textSize="10dp"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="92dp"
android:textSize="12dp"
android:text="二"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="92dp"
android:textSize="12dp"
android:text="三"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="92dp"
android:textSize="12dp"
android:text="四"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="92dp"
android:textSize="12dp"
android:text="五"
android:gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="92dp"
android:textSize="12dp"
android:text="六"
android:gravity="center"/>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#E5E5E5"/>
<GridView
android:id="@+id/courceDetail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="7"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp"
android:stretchMode="columnWidth"
android:background="#E5E5E5">
</GridView>
</LinearLayout>
</ScrollView>
</LinearLayout>
下面是GridView的适配器代码:
public class MyAdapter extends BaseAdapter {
private Context mContext;
//保存内容的内部数组
private List<String> content;
public MyAdapter(Context context, List<String> list) {
this.mContext = context;
this.content = list;
}
public int getCount() {
return content.size();
}
public Object getItem(int position) {
return content.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if( convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.grib_item, null);
}
TextView textView = (TextView)convertView.findViewById(R.id.text);
//如果有课,那么添加数据
if( !getItem(position).equals("")) {
textView.setText((String)getItem(position));
textView.setTextColor(Color.WHITE);
//变换颜色
int rand = position % 7;
switch( rand ) {
case 0:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.grid_item_bg));
break;
case 1:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_12));
break;
case 2:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_13));
break;
case 3:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_14));
break;
case 4:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_15));
break;
case 5:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_16));
break;
case 6:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_17));
break;
case 7:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_18));
break;
}
}
return convertView;
}
}
下面来慢慢解释一下,首先,要自定义适配器必须要继承一个Adapter,这里我们从BaseAdapter继承,继承之后必须要重写getCount(),getItem(int),getItemId(int),getView(int,View,ViewGroup) 这些方法必须要重写,最主要的就是重写getView() 这个方法,因为这个方法返回的是一个View,那么就是GridView的每一个子item的布局。
convertView=LayoutInflater.from(mContext).inflate(R.layout.grib_item, null);
这一行代码是加载布局文件并返回一个View
if( !getItem(position).equals("")) {
textView.setText((String)getItem(position));
textView.setTextColor(Color.WHITE);
//变换颜色
int rand = position % 7;
switch( rand ) {
case 0:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.grid_item_bg));
break;
case 1:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_12));
break;
case 2:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_13));
break;
case 3:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_14));
break;
case 4:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_15));
break;
case 5:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_16));
break;
case 6:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_17));
break;
case 7:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_18));
break;
}
}
这里的代码是判断每一列然后实现更改view的背景,其中背景的代码如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#75B7A0" />
<corners android:radius="3dip" />
</shape>
其他的类似,还有就是item的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F4FFF5EE">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:padding="2dp"
android:textSize="12dp"
android:gravity="center"/>
</RelativeLayout>
这个就是模仿课程表的布局,最后附上源码Git地址:点我下载
以上所述是小编给大家介绍的android开发 * 课程表的布局实例详解网站的支持!
猜你喜欢
- 本文实例为大家分享了C#实现影院售票系统的具体代码,供大家参考,具体内容如下本人认为此项目的难点有4点1.首先是将解析完的XML文件绑定到T
- 初看 cgaolei 翻译的 Java技巧之双括弧初始化 一文,走马观花,只知用法,未细看后面的解释。蔚为惊艳,心里想 Java 竟然有这么
- 前言图形相交检测常常用在伤害判定,使用自定义的图形相交检测,可以在一定程度上控制性能。比如2D格斗游戏中使用的矩形包围盒(AABB),一些动
- 文件上传概述实现web开发中的文件上传功能,需完成如下二步操作:在web页面中添加上传输入项在servlet中读取上传文件的数据,并保存到本
- 有些时候,你必须把某些文件放到Git工作目录中,但又不能提交它们,比如保存了数据库密码的配置文件啦,等等,每次git status都会显示U
- 一、什么是稀疏数组当一个数组a中大部分元素为0,或者为同一个值,那么可以用稀疏数组b来保存数组a。首先,稀疏数组是一个数组,然后以一种特定的
- 1、maven是什么,为什么存在?项目结构是什么样子,怎么定位jar官方网站说了好多,整的多复杂一样,简单说:maven是一个管理包的工具。
- 一、MyBatisPlusConfig中配置分页插件/** * 配置分页插件 * @
- 前言 同源策略:判断是否是同源的,主要看这三点,协议,ip,端口。同源策略就是浏览器出于网站安全性的考虑,限制不同源之间的资源相互访问的一种
- 1.微信配置信息 global.properties2.方法wxpay用于生成预支付订单信息方法notifyWeiXinPay用于微信支付成
- 需求使用 spring-boot 项目开发中,项目启动时“非常”慢的。如果每次修改代码或静态资源文件后都需要重新启动项目,这是多么痛苦的事。
- 前言说起整合自然离不开ssm,我本身并不太喜欢ORM,尤其是MyBatis,把SQL语句写在xml里,尤其是大SQL,可读性不高,出错也不容
- ThymeleafThymeleaf是最近SpringBoot推荐支持的模板框架,官网在thymeleaf.org这里。我们为什么要用Thy
- hello, everyone. Long time no see. 本期文章,我们主要讲解一下二叉树的相关概念,顺便也把搜索二叉树(也叫二
- 本文实例为大家分享了android自定义imageview实现圆角图片的具体代码,供大家参考,具体内容如下自定义图片的属性,对图片进行圆角切
- 背景在实际开发过程中,会遇到需要编写各类打印模板模板的需求,当然这些在WPF开发中更为常见,但是使用XAML写编辑的打印模板又不能直接发送给
- 本文实例讲述了C#全局热键设置与窗体热键设置,分享给大家供大家参考。具体实现方法如下:1、窗体热键首先要设置主窗体KeyPreview为tr
- 本文实例讲述了C#通过xpath查找xml指定元素的方法。分享给大家供大家参考。具体如下:orders.xml文档内容如下<?xml
- 本文实例讲述了Java通过在主循环中判断Boolean来停止线程的方法。分享给大家供大家参考,具体如下:package Threads;/*
- 前言本篇文章 中写到的是 flutter 调用了Android 原生的 TextView 案例添加原生组件的流程基本上可以描述为:1 and