Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 高仿課程表布局實作詳解

編輯:關於 Android 程式設計

這個範例會實作一個類似課程表的畫面,包含年份、週次選擇、星期標題、節次標示,以及以 GridView 呈現課程內容。整體結構適合拿來練習自訂 View、Adapter 與多層 Layout 組合。

參考畫面:

版面分析

畫面可以分成幾個區塊:

  1. 頂部:顯示年份與週次選擇。
  2. 星期標題:顯示一到日。
  3. 左側:顯示節次。
  4. 右側:使用 GridView 顯示課程格。

自訂星期標題 View

星期標題可以自訂 View,在 onDraw 中繪製七個文字。

@Override
protected void onDraw(Canvas canvas) {
    int width = getWidth();
    int offset = width / 8;
    int currentPosition = offset;

    mPaint.setTypeface(Typeface.create(Typeface.DEFAULT_BOLD, Typeface.BOLD));
    mPaint.setTextSize(30);
    mPaint.setColor(Color.rgb(0, 134, 139));

    String[] days = {"一", "二", "三", "四", "五", "六", "日"};

    for (int i = 0; i < 7; i++) {
        canvas.drawText(days[i], currentPosition, 30, mPaint);
        currentPosition += offset;
    }

    super.onDraw(canvas);
}

主要布局結構

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/year"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:text="2016年" />

        <Spinner
            android:id="@+id/switchWeek"
            android:layout_width="match_parent"
            android:layout_height="50dp" />
    </LinearLayout>

    <cn.example.demo.WeekTitle
        android:layout_width="match_parent"
        android:layout_height="30dp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="25dp"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView android:text="一" ... />
                <TextView android:text="二" ... />
                <TextView android:text="三" ... />
                <TextView android:text="四" ... />
                <TextView android:text="五" ... />
                <TextView android:text="六" ... />
            </LinearLayout>

            <GridView
                android:id="@+id/courceDetail"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:numColumns="7" />
        </LinearLayout>
    </ScrollView>
</LinearLayout>

GridView Adapter

課程格使用自訂 BaseAdapter,在 getView 中載入每一格的版面,並依據資料設定背景與文字。

public class MyAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> content;

    public MyAdapter(Context context, List<String> list) {
        this.mContext = context;
        this.content = list;
    }

    @Override
    public int getCount() {
        return content.size();
    }

    @Override
    public Object getItem(int position) {
        return content.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext)
                .inflate(R.layout.grib_item, null);
        }

        TextView textView = convertView.findViewById(R.id.text);
        String item = getItem(position).toString();

        if (!item.equals("")) {
            textView.setText(item);
            textView.setTextColor(Color.WHITE);
        }

        return convertView;
    }
}

課程格背景可以使用 shape 資源:

<shape xmlns:android="https://schemas.android.com/apk/res/android">
    <solid android:color="#75B7A0" />
    <corners android:radius="3dp" />
</shape>

這個課程表布局的關鍵在於合理的層級安排,以及自訂 Adapter 對 GridView 每一格的呈現控制。完成後即可得到接近真實課表 App 的視覺效果。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved