前言

在做之前那个查课app时,用到了课程表显示,当然,找资料也费了些功夫,还是自己没做过,经验也少,所以想分享下。

正文

在新建工程后,本文仅给出界面文件和实现代码。

新建Activity,可以命名为zhuanyekebiao。
界面代码为:

<?xml version="1.0" encoding="utf-8"?>"http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent" >    "@+id/test_empty"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        style="@style/courseTableText"        android:text="@string/empty"        android:background="@drawable/course_text_view_bg"        />    "@+id/test_monday_course"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        style="@style/courseTableText"        android:text="@string/mon"        android:layout_toRightOf="@id/test_empty"        android:background="@drawable/course_text_view_bg"        />    "@+id/test_tuesday_course"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/tue"        style="@style/courseTableText"        android:layout_toRightOf="@id/test_monday_course"        android:background="@drawable/course_text_view_bg"        />    "@+id/test_wednesday_course"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/wen"        style="@style/courseTableText"        android:layout_toRightOf="@id/test_tuesday_course"        android:background="@drawable/course_text_view_bg"        />    "@+id/test_thursday_course"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/thu"        style="@style/courseTableText"        android:layout_toRightOf="@id/test_wednesday_course"        android:background="@drawable/course_text_view_bg"        />    "@+id/test_friday_course"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/fri"        style="@style/courseTableText"        android:layout_toRightOf="@id/test_thursday_course"        android:background="@drawable/course_text_view_bg"        />    "@+id/test_saturday_course"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/sta"        style="@style/courseTableText"        android:layout_toRightOf="@id/test_friday_course"        android:background="@drawable/course_text_view_bg"        />    "@+id/test_sunday_course"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        style="@style/courseTableText"        android:text="@string/sun"        android:layout_toRightOf="@id/test_saturday_course"        android:background="@drawable/course_table_last_colum"        />    "@+id/scroll_body"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@id/test_empty"        android:scrollbars="none"        >        "fill_parent"            android:layout_height="wrap_content"            android:id="@+id/test_course_rl"            >        </RelativeLayout>    ScrollView></RelativeLayout>

具体实现代码如下:

package com.bestyou.chake;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.util.DisplayMetrics;import android.view.Gravity;import android.view.Menu;import android.widget.RelativeLayout;import android.widget.TextView;import java.util.Random;public class zhuanyekebiao extends Activity {    /** 第一个无内容的格子 */    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,4,6,8};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_zhuanyekebiao);        //获得列头的控件        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 <= 10; i ++){            for(int j = 1; j <= 8; j ++){                TextView tx = new TextView(zhuanyekebiao.this);                tx.setId((i - 1) * 8  + j);                //除了最后一列,都使用course_text_view_bg背景(最后一列没有右边框)                if(j < 8)                    tx.setBackgroundDrawable(zhuanyekebiao.this.                            getResources().getDrawable(R.drawable.course_text_view_bg));                else                    tx.setBackgroundDrawable(zhuanyekebiao.this.                            getResources().getDrawable(R.drawable.course_table_last_colum));                //相对布局参数                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],"地图制图基础\n9-19(3,4)\n于焕菊\nJ14-305室");        setCourseMessage(2,jieci[1],"大学英语(3-3)\n1-19(3,4)\n徐育新\nJ1-310室");        setCourseMessage(3,jieci[1],"概率论与数理统计\n1-9(3,4)\n郑艳林\nJ1-117室");        setCourseMessage(3,jieci[1],"概率论与数理统计\n1-9(3,4)\n郑艳林\nJ1-117室");        setCourseMessage(4,jieci[1],"线性代数\n1-11(3,4)\n王新赠\nJ14-121室");        setCourseMessage(5,jieci[1],"大学物理(B)(2-2)\n1-19(3,4)\n周明东\nJ1-307室");        setCourseMessage(6,jieci[0],"大学物理(B)(2-2)\n1-19(3,4)\n周明东\nJ1-307室");        setCourseMessage(7,jieci[2],"大学物理(B)(2-2)\n1-19(3,4)\n周明东\nJ1-307室");        setCourseMessage(1,jieci[0],"地图制图基础\n9-19(3,4)\n于焕菊\nJ14-305室");        setCourseMessage(2,jieci[2],"大学英语(3-3)\n1-19(3,4)\n徐育新\nJ1-310室");        setCourseMessage(3,jieci[3],"概率论与数理统计\n1-9(3,4)\n郑艳林\nJ1-117室");        setCourseMessage(4,jieci[2],"线性代数\n1-11(3,4)\n王新赠\nJ14-121室");        setCourseMessage(5,jieci[0],"大学物理(B)(2-2)\n1-19(3,4)\n周明东\nJ1-307室");    }    @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_red,                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 = 1;        // 偏移由这节课是星期几决定        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);    }}

总结

仅作显示,还是很简单的。当然,如果实用的话,还要加入网络通信。

更多相关文章

  1. android 音量控制setVolumeControlStream
  2. Android6.0-新控件(一)
  3. 修改Android默认启动项launcher
  4. [RK3288]串口开发之JNI环境搭建(基于android8.1使用android studi
  5. Android时间转换星期 昨天 今天工具
  6. Android应用之Hybird混合开发,集成web页面的方法尝试
  7. Android自定义Dialog实现通用圆角对话框
  8. [置顶] activity配置信息详解
  9. 从TabLayout源码告诉你使用它的正确姿势,让你马上爱上它

随机推荐

  1. 机器学习算法-随机森林(Random Forest)
  2. CSS学习(一)
  3. Linux管理员和普通用户
  4. css元素选择器
  5. 一、简单学习Ajax的GET & POST请求
  6. 干货丨手把手教你如何加载和操作DolphinD
  7. Terraform踩坑记之:Azure Provider配置
  8. 模拟器如何换IP
  9. virt-viewer编译:syntax error near unexp
  10. 马斯克的星链计划对互联网有哪些影响?