课程表要做复杂要考虑的东西很多,这里只说实现界面的一个简单的思路:ScrollView嵌套七个(一周七天)LinearLayout, 而每个LinearLayout又能封装成一个课程表View的ViewItem,都继承自FrameLayout。下面上代码:
首先是课程表每一天的item,现在默认一天是7节课,后面可设置成可拓展用代码实现,这里现在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="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_school_timetable_item_day"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Mon"
android:textColor="@color/white"
android:background="@color/black"
android:gravity="center"
android:textSize="20sp"
/>
<TextView
android:id="@+id/tv_school_timetable_item_class1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="语文"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center"
android:textSize="18sp"
/>
<TextView
android:id="@+id/tv_school_timetable_item_class2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="语文"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center"
android:textSize="18sp"
android:layout_marginTop="1dp"
/>
<TextView
android:id="@+id/tv_school_timetable_item_class3"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="语文"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center"
android:textSize="18sp"
android:layout_marginTop="1dp"
/>
<TextView
android:id="@+id/tv_school_timetable_item_class4"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="语文"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center"
android:textSize="18sp"
android:layout_marginTop="1dp"
/>
<TextView
android:id="@+id/tv_school_timetable_item_class5"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="语文"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center"
android:textSize="18sp"
android:layout_marginTop="1dp"
/>
<TextView
android:id="@+id/tv_school_timetable_item_class6"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="语文"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center"
android:textSize="18sp"
android:layout_marginTop="1dp"
/>
<TextView
android:id="@+id/tv_school_timetable_item_class7"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="语文"
android:textColor="@color/black"
android:background="@color/white"
android:gravity="center"
android:textSize="18sp"
android:layout_marginTop="1dp"
/>
</LinearLayout>
就是竖着排8个TextView,第一个text用来显示星期几,后面的就是显示课程名称,如果复杂,可以把TextView换成一个包含多个View的Layout,尺寸大小都可以通过代码来控制,这里不展开。然后是item控件的代码:
package com.amuro.timetable;
import com.amuro.schooltimetabletest.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextView;
public class SchoolTimetableItem extends FrameLayout
{
private TextView textViewDay;
private TextView textViewClass1;
private TextView textViewClass2;
private TextView textViewClass3;
private TextView textViewClass4;
private TextView textViewClass5;
private TextView textViewClass6;
private TextView textViewClass7;
public SchoolTimetableItem(Context context, AttributeSet attrs)
{
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.school_timetable_item_layout, this);
initView();
}
private void initView()
{
textViewDay = (TextView)findViewById(R.id.tv_school_timetable_item_day);
textViewClass1 = (TextView)findViewById(R.id.tv_school_timetable_item_class1);
textViewClass2 = (TextView)findViewById(R.id.tv_school_timetable_item_class2);
textViewClass3 = (TextView)findViewById(R.id.tv_school_timetable_item_class3);
textViewClass4 = (TextView)findViewById(R.id.tv_school_timetable_item_class4);
textViewClass5 = (TextView)findViewById(R.id.tv_school_timetable_item_class5);
textViewClass6 = (TextView)findViewById(R.id.tv_school_timetable_item_class6);
textViewClass7 = (TextView)findViewById(R.id.tv_school_timetable_item_class7);
}
public void setDay(String day)
{
textViewDay.setText(day);
}
public void setClasses(String[] classes)
{
textViewClass1.setText(classes[0]);
textViewClass2.setText(classes[1]);
textViewClass3.setText(classes[2]);
textViewClass4.setText(classes[3]);
textViewClass5.setText(classes[4]);
textViewClass6.setText(classes[5]);
textViewClass7.setText(classes[6]);
}
}
代码有点low,可以用list来简化,不要在意这些细节。
下一步就是封装成课程表控件,上代码和布局文件:
package com.amuro.timetable;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import com.amuro.schooltimetabletest.R;
public class SchoolTimetable extends FrameLayout
{
private String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
private String[][] classes = {
{"语", "", "数", "", "", "英", ""},
{"物", "", "", "英", "", "", "数"},
{"", "", "化", "", "", "", ""},
{"生", "", "", "", "语", "", "数"},
{"", "物", "", "英", "", "", ""},
{"", "语", "化", "", "", "", ""},
{"数", "", "", "英", "", "", ""}};
public SchoolTimetable(Context context, AttributeSet attrs)
{
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.school_timetable_layout, this);
initView();
}
private void initView()
{
int startId = R.id.sti_school_timetable_1;
for(int i = 0;i < 7;i++)
{
SchoolTimetableItem sti = (SchoolTimetableItem)findViewById(startId + i);
sti.setDay(days[i]);
sti.setClasses(classes[i]);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<com.amuro.timetable.SchoolTimetableItem
android:id="@+id/sti_school_timetable_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<com.amuro.timetable.SchoolTimetableItem
android:id="@+id/sti_school_timetable_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="1dp"
/>
<com.amuro.timetable.SchoolTimetableItem
android:id="@+id/sti_school_timetable_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="1dp"
/>
<com.amuro.timetable.SchoolTimetableItem
android:id="@+id/sti_school_timetable_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="1dp"
/>
<com.amuro.timetable.SchoolTimetableItem
android:id="@+id/sti_school_timetable_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="1dp"
/>
<com.amuro.timetable.SchoolTimetableItem
android:id="@+id/sti_school_timetable_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="1dp"
/>
<com.amuro.timetable.SchoolTimetableItem
android:id="@+id/sti_school_timetable_7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="1dp"
/>
</LinearLayout>
</ScrollView>
把课程表和课程表item分离是为了两边可以分别拓展功能,做好解耦,熟悉偶的朋友都知道这是偶的强迫症。
最后,很多朋友肯定会说:
既然你们都这么说了,那我就不上了,谢谢大家,逃~