转载请标明出处:http://blog.csdn.net/collect1988/article/details/46953821,本文出自【 cxbiaomy 的博客】
主要是使用ScrollView和LinearLayout来实现,比较简单
下面是效果图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:gravity="center"
android:text="7月"
android:textColor="#ffffff"
android:textSize="10sp" />
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期一"
android:textColor="#ffffff"
android:textSize="10sp" />
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期二"
android:textColor="#ffffff"
android:textSize="10sp" />
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期三"
android:textColor="#ffffff"
android:textSize="10sp" />
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期四"
android:textColor="#ffffff"
android:textSize="10sp" />
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期五"
android:textColor="#ffffff"
android:textSize="10sp" />
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期六"
android:textColor="#ffffff"
android:textSize="10sp" />
<TextView
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:gravity="center"
android:text="星期日"
android:textColor="#ffffff"
android:textSize="10sp" />
LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadingEdge="none"
android:scrollbars="none"
android:overScrollMode="never" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="1"
android:textColor="#666666"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="2"
android:textColor="#666666"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="3"
android:textColor="#666666"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="4"
android:textColor="#666666"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="5"
android:textColor="#666666"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="6"
android:textColor="#666666"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="7"
android:textColor="#666666"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="8"
android:textColor="#666666"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="9"
android:textColor="#666666"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="10"
android:textColor="#666666"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="11"
android:textColor="#666666"
android:textSize="12sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:text="12"
android:textColor="#666666"
android:textSize="12sp" />
LinearLayout>
<LinearLayout
android:id="@+id/ll1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/ll2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/ll3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/ll4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/ll5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/ll6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/ll7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
LinearLayout>
ScrollView>
LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
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:textColor="#ffffff"
android:textSize="12sp"/>
<TextView
android:id="@+id/place"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="10sp"/>
<TextView
android:id="@+id/last"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:textColor="#ffffff"
android:textSize="10sp"/>
<TextView
android:id="@+id/time"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="13dp"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#ffffff"
android:textSize="10sp"/>
LinearLayout>
package com.bryan;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class TableDemoActivity extends Activity {
private int colors[] = {
Color.rgb(0xee,0xff,0xff),
Color.rgb(0xf0,0x96,0x09),
Color.rgb(0x8c,0xbf,0x26),
Color.rgb(0x00,0xab,0xa9),
Color.rgb(0x99,0x6c,0x33),
Color.rgb(0x3b,0x92,0xbc),
Color.rgb(0xd5,0x4d,0x34),
Color.rgb(0xcc,0xcc,0xcc)
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//分别表示周一到周日
LinearLayout ll1 = (LinearLayout)findViewById(R.id.ll1);
LinearLayout ll2 = (LinearLayout)findViewById(R.id.ll2);
LinearLayout ll3 = (LinearLayout)findViewById(R.id.ll3);
LinearLayout ll4 = (LinearLayout)findViewById(R.id.ll4);
LinearLayout ll5 = (LinearLayout)findViewById(R.id.ll5);
LinearLayout ll6 = (LinearLayout)findViewById(R.id.ll6);
LinearLayout ll7 = (LinearLayout)findViewById(R.id.ll7);
//每天的课程设置
setNoClass(ll1, 2, 0);
setClass(ll1, "windows编程实践", "国软 4-503", "1-9周,每一周", "9:50-11:25", 2, 1);
setNoClass(ll1, 3, 0);
setClass(ll1, "概率论与数理统计", "国软 4-304", "1-15周,每一周", "14:55-17:25", 3, 2);
setNoClass(ll1, 2, 0);
setClass(ll2, "大学英语", "国软 4-302", "1-18周,每一周", "8:00-9:35", 2, 3);
setClass(ll2, "计算机组织体系与结构", "国软 4-204", "1-15,每一周", "9:50-12:15", 3, 5);
setNoClass(ll2, 3, 0);
setClass(ll2, "团队激励和沟通", "国软 4-204", "1-9周,每一周", "15:45-17:25", 2, 6);
setNoClass(ll2, 2, 0);
setNoClass(ll3, 2, 0);
setClass(ll3, "中国近现代史纲要", "3区 1-328", "1-9周,每一周", "9:50-12:15", 3, 1);
setNoClass(ll3, 1, 0);
setClass(ll3, "体育(网球)", "信息学部 操场", "6-18周,每一周", "14:00-15:40", 2, 2);
setNoClass(ll3, 4, 0);
setClass(ll4, "计算机组织体系与结构", "国软 4-204", "1-15,每一周", "8:00-9:35", 2, 5);
setClass(ll4, "数据结构与算法", "国软 4-304", "1-18周,每一周", "9:50-12:15", 3, 4);
setNoClass(ll4, 1, 0);
setClass(ll4, "面向对象程序设计(JAVA)", "国软 1-103", "1-18周,每一周", "14:00-16:30", 3, 5);
setNoClass(ll4, 3, 0);
setClass(ll5, "c#程序设计", "国软 4-102", "1-9周,每一周", "8:00-9:35", 2, 6);
setClass(ll5, "大学英语", "国软 4-302", "1-18周,每一周", "9:50-11:25", 2, 3);
setNoClass(ll5, 2, 0);
setClass(ll5, "基础物理", "国软 4-304", "1-18周,每一周", "14:00-16:30", 3, 1);
setNoClass(ll5, 3, 0);
setNoClass(ll6, 12, 0);
setNoClass(ll7, 12, 0);
}
/**
* 设置课程的方法
* @param ll
* @param title 课程名称
* @param place 地点
* @param last 时间
* @param time 周次
* @param classes 节数
* @param color 背景色
*/
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);
int height=dip2px(this,classes * 50); //48
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,height);
view.setLayoutParams(params);
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);
}
/**
* 设置无课(空百)
* @param ll
* @param classes 无课的节数(长度)
* @param color
*/
void setNoClass(LinearLayout ll,int classes, int color)
{
TextView blank = new TextView(this);
if(color == 0)
blank.setMinHeight(dip2px(this,classes * 50));
else
blank.setBackgroundColor(colors[color]);
ll.addView(blank);
}
//点击课程的监听器
class OnClickClassListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
String title;
title = (String) ((TextView)v.findViewById(R.id.title)).getText();
Toast.makeText(getApplicationContext(), "你点击的是:" + title,
Toast.LENGTH_SHORT).show();
}
}
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);} /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);}
}
主要是设置有课的数量和没课的数量
源码点击下载