Android小工具自定义view课表
作者:Rose 发布时间:2023-02-12 14:51:41
标签:Android,view,课表
本文实例为大家分享了Android自定义view课表的具体代码,供大家参考,具体内容如下
这里是模拟课表,数据写死了的,不过也可以通过抓包获取教务系统课表
1.xml文件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#A5D6A7"
android:fitsSystemWindows="true"
android:layout_height="match_parent"
tools:context=".KebiaoActivity">
<!-- <com.baidu.lbsapi.panoramaview.PanoramaView-->
<!-- android:id="@+id/panorama"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- android:clickable="true" />-->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/back"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginStart="15dp"
app:layout_constraintBottom_toBottomOf="@+id/textView16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView16"
app:srcCompat="@mipmap/fanhui"
android:layout_marginLeft="15dp" />
<TextView
android:id="@+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="课表"
android:textColor="#ffffff"
android:textSize="26sp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/ershou"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="本周课表"
android:textColor="@color/white"
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.45" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Schedule of the week"
android:textColor="@color/white"
android:textSize="20sp"
android:layout_marginTop="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/ershou" />
</androidx.constraintlayout.widget.ConstraintLayout>
<RelativeLayout
android:id="@+id/kebiao22"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout">
<TextView android:id="@+id/test_empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11\n月"
android:gravity="center"
android:background="@drawable/course_text_view_bg"
/>
<TextView android:id="@+id/test_monday_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="23日\n周一"
android:layout_toRightOf="@id/test_empty"
android:background="@drawable/course_text_view_bg"
android:gravity="center"
/>
<TextView android:id="@+id/test_tuesday_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="24日\n周二"
android:layout_toRightOf="@id/test_monday_course"
android:background="@drawable/course_text_view_bg"
android:gravity="center"
/>
<TextView android:id="@+id/test_wednesday_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="25日\n周三"
android:layout_toRightOf="@id/test_tuesday_course"
android:background="@drawable/course_text_view_bg"
android:gravity="center"
/>
<TextView android:id="@+id/test_thursday_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="26日\n周四"
android:layout_toRightOf="@id/test_wednesday_course"
android:background="@drawable/course_text_view_bg"
android:gravity="center"
/>
<TextView android:id="@+id/test_friday_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="27日\n周五"
android:layout_toRightOf="@id/test_thursday_course"
android:background="@drawable/course_text_view_bg"
android:gravity="center"
/>
<TextView android:id="@+id/test_saturday_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="28日\n周六"
android:layout_toRightOf="@id/test_friday_course"
android:background="@drawable/course_text_view_bg"
android:gravity="center"
/>
<TextView android:id="@+id/test_sunday_course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="29日\n周日"
android:layout_toRightOf="@id/test_saturday_course"
android:background="@drawable/course_text_view_bg"
android:gravity="center"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="800dp"
android:id="@+id/test_course_rl"
app:layout_constraintTop_toBottomOf="@id/kebiao22"
>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
course_text_view_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#ffffff"/>
<stroke android:width="0.2dp"
android:color="#AEAAAA"/>
</shape>
2.activity代码
public class KebiaoActivity extends AppCompatActivity {
/** 第一个无内容的格子 */
protected TextView empty;
/** 星期一的格子 */
protected TextView monColum;
/** 星期二的格子 */
protected TextView tueColum;
/** 星期三的格子 */
protected TextView wedColum;
/** 星期四的格子 */
protected TextView thrusColum;
/** 星期五的格子 */
protected TextView friColum;
/** 星期六的格子 */
protected TextView satColum;
/** 星期日的格子 */
protected TextView sunColum;
/** 课程表body部分布局 */
protected RelativeLayout course_table_layout;
/** 屏幕宽度 **/
protected int screenWidth;
/** 课程格子平均宽度 **/
protected int aveWidth;
int gridHeight1 = 0;
//(0)对应12节;(2)对应34节;(4)对应56节;(6)对应78节;(8)对应于9 10节
int[] jieci = {0,2,3,5,4,6,8};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= 21){
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
getWindow().setStatusBarColor(Color.TRANSPARENT);
}
setContentView(R.layout.activity_kebiao);
ImageView mImageView=findViewById(R.id.back);
mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
//获得列头的控件
empty = (TextView) this.findViewById(R.id.test_empty);
monColum = (TextView) this.findViewById(R.id.test_monday_course);
tueColum = (TextView) this.findViewById(R.id.test_tuesday_course);
wedColum = (TextView) this.findViewById(R.id.test_wednesday_course);
thrusColum = (TextView) this.findViewById(R.id.test_thursday_course);
friColum = (TextView) this.findViewById(R.id.test_friday_course);
satColum = (TextView) this.findViewById(R.id.test_saturday_course);
sunColum = (TextView) this.findViewById(R.id.test_sunday_course);
course_table_layout = (RelativeLayout) this.findViewById(R.id.test_course_rl);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
//屏幕宽度
int width = dm.widthPixels;
//平均宽度
int aveWidth = width / 8;
//第一个空白格子设置为25宽
empty.setWidth(aveWidth * 3/4);
monColum.setWidth(aveWidth * 33/32 + 1);
tueColum.setWidth(aveWidth * 33/32 + 1);
wedColum.setWidth(aveWidth * 33/32 + 1);
thrusColum.setWidth(aveWidth * 33/32 + 1);
friColum.setWidth(aveWidth * 33/32 + 1);
satColum.setWidth(aveWidth * 33/32 + 1);
sunColum.setWidth(aveWidth * 33/32 + 1);
this.screenWidth = width;
this.aveWidth = aveWidth;
int height = dm.heightPixels;
int gridHeight = height / 10;
gridHeight1 = gridHeight;
//设置课表界面
//动态生成10 * maxCourseNum个textview
for(int i = 1; i <= 12; i ++){
for(int j = 1; j <= 8; j ++){
TextView tx = new TextView(KebiaoActivity.this);
tx.setId((i - 1) * 8 + j);
//除了最后一列,都使用course_text_view_bg背景(最后一列没有右边框)
tx.setBackgroundDrawable(KebiaoActivity.this.
getResources().getDrawable(R.drawable.course_text_view_bg));
//相对布局参数
RelativeLayout.LayoutParams rp = new RelativeLayout.LayoutParams(
aveWidth * 33 / 32 + 1,
gridHeight);
//文字对齐方式
tx.setGravity(Gravity.CENTER);
//字体样式
// tx.setTextAppearance(this, R.style.courseTableText);
// //如果是第一列,需要设置课的序号(1 到 12)
if(j == 1)
{
tx.setText(String.valueOf(i));
rp.width = aveWidth * 3/4;
//设置他们的相对位置
if(i == 1)
rp.addRule(RelativeLayout.BELOW, empty.getId());
else
rp.addRule(RelativeLayout.BELOW, (i - 1) * 8);
}
else
{
rp.addRule(RelativeLayout.RIGHT_OF, (i - 1) * 8 + j - 1);
rp.addRule(RelativeLayout.ALIGN_TOP, (i - 1) * 8 + j - 1);
tx.setText("");
}
tx.setLayoutParams(rp);
course_table_layout.addView(tx);
}
}
setCourseMessage(1,jieci[1],"软件工\n程@含\n浦校区\n3508教\n室\n");
setCourseMessage2(2,jieci[0],"嵌入式\n系统...\n@含浦\n校区\n3204教\n室");
setCourseMessage(3,jieci[1],"计算机\n图形\n学@含\n浦校区\n3310教\n室");
setCourseMessage2(4,jieci[0],"计算机\n网络...\n@含浦\n校区\n3311教\n室");
setCourseMessage2(5,jieci[0],"嵌入式\n系统...\n@h含浦\n校区嵌入\n式实验室");
setCourseMessage(5,jieci[1],"java...\n@h含浦\n校区12\n机房\n(65)");
setCourseMessage(1,jieci[3],"人工智\n能原\n理@含\n浦校区\n3306教\n");
setCourseMessage2(2,jieci[2],"计算机\n网络...\n@含浦\n校区\n2204教\n室");
setCourseMessage(5,jieci[3],"操作系\n统原\n理@含\n浦校区\n12机房\n(65)");
}
//菜单
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
// return true;
// }
public void setCourseMessage(int xingqi,int jieci,String courseMessage){
//五种颜色的背景
int[] background = {R.drawable.course_info_blue, R.drawable.course_info_green,
R.drawable.course_info_red, R.drawable.course_info_zisi,
R.drawable.course_info_yellow};
// 添加课程信息
TextView courseInfo = new TextView(this);
courseInfo.setText(courseMessage);
//该textview的高度根据其节数的跨度来设置
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
aveWidth * 31 / 32,
(gridHeight1 - 5) * 3 );
//textview的位置由课程开始节数和上课的时间(day of week)确定
rlp.topMargin = 5 + jieci * gridHeight1;
rlp.leftMargin = 5;
// 偏移由这节课是星期几决定
rlp.addRule(RelativeLayout.RIGHT_OF, xingqi);
//字体剧中
courseInfo.setGravity(Gravity.CENTER);
// 设置一种背景
Random random = new Random();
courseInfo.setBackgroundResource(background[random.nextInt(5)]);
courseInfo.setTextSize(12);
courseInfo.setLayoutParams(rlp);
courseInfo.setTextColor(Color.WHITE);
//设置不透明度
courseInfo.getBackground().setAlpha(222);
course_table_layout.addView(courseInfo);
}
public void setCourseMessage2(int xingqi,int jieci,String courseMessage){
//五种颜色的背景
int[] background = {R.drawable.course_info_blue, R.drawable.course_info_green,
R.drawable.course_info_red, R.drawable.course_info_zisi,
R.drawable.course_info_yellow};
// 添加课程信息
TextView courseInfo = new TextView(this);
courseInfo.setText(courseMessage);
//该textview的高度根据其节数的跨度来设置
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
aveWidth * 31 / 32,
(gridHeight1 - 5) * 2 );
//textview的位置由课程开始节数和上课的时间(day of week)确定
rlp.topMargin = 5 + jieci * gridHeight1;
rlp.leftMargin = 5;
// 偏移由这节课是星期几决定
rlp.addRule(RelativeLayout.RIGHT_OF, xingqi);
//字体剧中
courseInfo.setGravity(Gravity.CENTER);
// 设置一种背景
Random random = new Random();
courseInfo.setBackgroundResource(background[random.nextInt(5)]);
courseInfo.setTextSize(12);
courseInfo.setLayoutParams(rlp);
courseInfo.setTextColor(Color.WHITE);
//设置不透明度
courseInfo.getBackground().setAlpha(222);
course_table_layout.addView(courseInfo);
}
}
3.资源文件
course_info_blue
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#4FC3F7"/>
</shape>
course_info_green
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#81C784"/>
</shape>
course_info_red
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#E57373"/>
</shape>
course_info_zisi
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#9575CD"/>
</shape>
course_info_yellow
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<solid android:color="#FFB74D"/>
</shape>
4.运行
来源:https://blog.csdn.net/qq_46526828/article/details/111228101
0
投稿
猜你喜欢
- 一、前言上一篇文章中我们已经Spring Boot 利用注解方式整合 MyBatis,今天我们就来看看,如何利
- 本文实例为大家分享了百度人脸识别之人脸识别FaceIdentify,供大家参考,具体内容如下using System.Collections
- 目录前言令牌中继令牌难道不能在Feign自动中继吗?实现令牌中继InheritableThreadLocal实现令牌中继总结前言在Sprin
- 本文实例讲述了java编程调用存储过程中得到新增记录id号的实现方法。分享给大家供大家参考,具体如下:关于ms sql server2000
- 需要记录日志的地方包括:进入方法的时候,传参的时候,统计执行时间,方法返回参数的时候,退出语句块的时候,出现异常的时候,等等。先来体验不使用
- 一、如何实现TCP通信要实现TCP通信需要创建一个服务器端程序和一个客户端程序,为了保证数据传输的安全性,首先需要实现服务器端程序,然后在编
- 今天给大家带来的是一块用WPF 实现魔方的小游戏,先看一下效果图 代码如下,先写一个类,用来判断是否可以移动using System;usi
- Android版本更新实例详解1、导入xutils的jar包 2、在AndroidManifest.xml中添加权限 3、选择下载的路径,和
- 逆转交替合并两个链表,即从一个链表的尾指针指向另一个链表的尾指针,依次逆转交替进行合并。下面就通过实例来详细的介绍该逆转交替合并两个链表的思
- instanceof 严格来说是Java中的一个双目运算符,用来测试一个对象是否为一个类的实例,用法为:boolean result = o
- 1.IO流介绍IO流可以用到的地方很多,就比如设计模式、下载、传输等等。学好IO流,为之后的进一步学习打下基础,那么,先来说说什么是流?流是
- servlet、filter、listener、interceptor之间的区别和联系一、概念1.servlet:servlet是一种运行服
- @JSONField看源码它可以作用于字段和方法上。引用网上说的,一、作用Field@JSONField作用在Field时,其name不仅定
- 1. 单个属性yaml 内的属性如下:server: port: 80只需在成员变量上注解 @Value(“
- 前言一般在c++中没有使用的变量会有警告,C#中也有,在QT中我们利用Q_UNSED可以直接消除这些警告,那么我们在C#中该如何做才能消除这
- 多线程下@Resource注入为null前情叙述记录以下这个坑,我的情况大致是这样的,我在用webmagic写爬虫,在类中通过@Resoou
- Filter简介Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所
- 在程序中对文件进行压缩解压缩是很重要的功能,不仅能减小文件的体积,还能对文件起到保护作用。如果是生成用户可以下载的文件,还可以极大的减少网络
- 1.前提:已安装JDK14,可以到jdk官网下载,或加QQ群835259695,快速免费下载2.不说没用的直接上jdk14环境下的jpack
- 前言  大部分的web开发者,开发的业务都是基于Http协议的:前端请求后端接口,携带参数,后端执行业务