Android 课程表的实现

最后实现的效果如下:

这里写图片描述


总体思路很简单:


先布置好容器
通过LayoutInflater来实例化出一个个模板对象,并赋值
将实例化的对象填到可见的容器中


至于左右和上下滚动那归功于ScrollView和HorizontalScrollView这两个标签



下面是布局XML文件,用到ScrollView 标签和HorizontalScrollView标签,其中HorizontalScrollView直接包含一个水平的线性布局,这样水平方向如果超出HorizontalScrollView的宽度,那么就可以水平方向滚动


这里写图片描述


下面是Activity中的一部分代码:

  LinearLayout ll1 = (LinearLayout)findViewById(R.id.ll1);
  setClass(ll1, "", "", "", "", 2, 0);
  setClass(ll1, "windows编程实践", "国软  4-503", "1-9周,每一周", "9:50-11:25", 2, 1);

  void setClass(LinearLayout ll, String title, String place,
            String last, String time, int classes, int color)
  {
        View view = LayoutInflater.from(this).inflate(R.layout.item, null);
        view.setMinimumHeight(dip2px(this,classes * 48));
        view.setBackgroundColor(colors[color]);
        ((TextView)view.findViewById(R.id.title)).setText(title);
        ((TextView)view.findViewById(R.id.place)).setText(place);
        ((TextView)view.findViewById(R.id.last)).setText(last);
        ((TextView)view.findViewById(R.id.time)).setText(time);
        //为课程View设置点击的监听器
        view.setOnClickListener(new OnClickClassListener());
        TextView blank1 = new TextView(this);
        TextView blank2 = new TextView(this);
        blank1.setHeight(dip2px(this,classes));
        blank2.setHeight(dip2px(this,classes));
        ll.addView(blank1);
        ll.addView(view);
        ll.addView(blank2);
 }
 void setNoClass(LinearLayout ll,int classes, int color)
 {
        TextView blank = new TextView(this);
        if(color == 0)
            blank.setMinHeight(dip2px(this,classes * 50));
        blank.setBackgroundColor(colors[color]);
        ll.addView(blank);
 }

下面是Item的一部分

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingRight="5dp"
    android:paddingLeft="5dp"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/title" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:textColor="#ffffff"
        android:textSize="16sp"/>
    <TextView 
        android:id="@+id/place"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:textColor="#FF4040"
        android:textSize="12sp"/>

FR:徐海涛(Hunk Xu) QQ技术群:386476712

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值