Android開(kāi)發(fā)高仿課程表的布局實(shí)例詳解
先說(shuō)下這個(gè)demo,這是一個(gè)模仿課程表的布局文件,雖然我是個(gè)菜鳥(niǎo),但我還是想留給學(xué)習(xí)的人一些例子,先看下效果
然后再來(lái)看一下我們學(xué)校的app

布局分析
先上一張劃分好了的布局圖
首先整個(gè)頁(yè)面放在一個(gè)LinearLayout布局下面,分為上面和下面兩個(gè)部分,下面一個(gè)是顯示課程表的詳細(xì)信息
1:這個(gè)沒(méi)什么好講的,就是直接一個(gè)LinearLayout布局,然后將控件一個(gè)TextView用來(lái)顯示年份,一個(gè)View用來(lái)當(dāng)作豎線,一個(gè)Spinner用來(lái)顯示選擇周數(shù)
2:這個(gè)是顯示星期幾的部件,是我自定義的View,自己重寫onDraw方法,然后畫出七個(gè)字,代碼如下:
public void onDraw(Canvas canvas)
{
//獲得當(dāng)前View的寬度
int width = getWidth();
int offset = width / 8;
int currentPosition = offset;
//設(shè)置要繪制的字體
mPaint.setTypeface(Typeface.create(Typeface.DEFAULT_BOLD, Typeface.BOLD));
mPaint.setTextSize(30);
mPaint.setColor(Color.rgb(0, 134, 139));
for(int i = 0; i < 7 ; i++)
{
//圈出當(dāng)前的日期
if( day == i)
{
System.out.println("畫出當(dāng)前的日期!");
}
canvas.drawText(days[i], currentPosition, 30, mPaint);
currentPosition += offset;
}
//調(diào)用父類的繪圖方法
super.onDraw(canvas);
}
3:這個(gè)也是一個(gè)LinearLayout,直接手寫布局,將寫出來(lái)的,將LinearLayout的orientation 屬性設(shè)置為vertical 。
4:這是一個(gè)GridView ,然后自己繼承BaseAdapter 填充內(nèi)容,下面放上布局的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="match_parent" android:orientation="vertical"> <!--顯示時(shí)間--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white"> <TextView android:id="@+id/year" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:layout_marginLeft="20dp" android:textSize="20dp" android:text="2016年"/> <!--豎線--> <View android:layout_width="1dp" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:background="#00FFFF" /> <!--下拉方式選周數(shù)--> <Spinner android:id="@+id/switchWeek" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:layout_gravity="center" /> </LinearLayout> <!--分隔線--> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#00FF7F"/> <!--顯示星期--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:background="@android:color/white"> <cn.karent.demo.UI.WeekTitle android:layout_width="match_parent" android:layout_height="30dp" android:layout_marginTop="10dp"/> </LinearLayout> <!--顯示課表詳細(xì)信息--> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!--顯示多少節(jié)課--> <LinearLayout android:layout_width="25dp" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="92dp" android:text="一" android:textSize="10dp" android:gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="92dp" android:textSize="12dp" android:text="二" android:gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="92dp" android:textSize="12dp" android:text="三" android:gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="92dp" android:textSize="12dp" android:text="四" android:gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="92dp" android:textSize="12dp" android:text="五" android:gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="92dp" android:textSize="12dp" android:text="六" android:gravity="center"/> </LinearLayout> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#E5E5E5"/> <GridView android:id="@+id/courceDetail" android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="7" android:horizontalSpacing="1dp" android:verticalSpacing="1dp" android:stretchMode="columnWidth" android:background="#E5E5E5"> </GridView> </LinearLayout> </ScrollView> </LinearLayout>
下面是GridView的適配器代碼:
public class MyAdapter extends BaseAdapter {
private Context mContext;
//保存內(nèi)容的內(nèi)部數(shù)組
private List<String> content;
public MyAdapter(Context context, List<String> list) {
this.mContext = context;
this.content = list;
}
public int getCount() {
return content.size();
}
public Object getItem(int position) {
return content.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if( convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.grib_item, null);
}
TextView textView = (TextView)convertView.findViewById(R.id.text);
//如果有課,那么添加數(shù)據(jù)
if( !getItem(position).equals("")) {
textView.setText((String)getItem(position));
textView.setTextColor(Color.WHITE);
//變換顏色
int rand = position % 7;
switch( rand ) {
case 0:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.grid_item_bg));
break;
case 1:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_12));
break;
case 2:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_13));
break;
case 3:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_14));
break;
case 4:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_15));
break;
case 5:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_16));
break;
case 6:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_17));
break;
case 7:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_18));
break;
}
}
return convertView;
}
}
下面來(lái)慢慢解釋一下,首先,要自定義適配器必須要繼承一個(gè)Adapter,這里我們從BaseAdapter繼承,繼承之后必須要重寫getCount(),getItem(int),getItemId(int),getView(int,View,ViewGroup) 這些方法必須要重寫,最主要的就是重寫getView() 這個(gè)方法,因?yàn)檫@個(gè)方法返回的是一個(gè)View,那么就是GridView的每一個(gè)子item的布局。
convertView=LayoutInflater.from(mContext).inflate(R.layout.grib_item, null);
這一行代碼是加載布局文件并返回一個(gè)View
if( !getItem(position).equals("")) {
textView.setText((String)getItem(position));
textView.setTextColor(Color.WHITE);
//變換顏色
int rand = position % 7;
switch( rand ) {
case 0:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.grid_item_bg));
break;
case 1:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_12));
break;
case 2:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_13));
break;
case 3:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_14));
break;
case 4:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_15));
break;
case 5:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_16));
break;
case 6:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_17));
break;
case 7:
textView.setBackground(mContext.getResources().getDrawable(R.drawable.bg_18));
break;
}
}
這里的代碼是判斷每一列然后實(shí)現(xiàn)更改view的背景,其中背景的代碼如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#75B7A0" /> <corners android:radius="3dip" /> </shape>
其他的類似,還有就是item的布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#F4FFF5EE"> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="80dp" android:layout_marginLeft="3dp" android:layout_marginRight="3dp" android:layout_marginTop="4dp" android:layout_marginBottom="4dp" android:padding="2dp" android:textSize="12dp" android:gravity="center"/> </RelativeLayout>
這個(gè)就是模仿課程表的布局,最后附上源碼Git地址:點(diǎn)我下載
以上所述是小編給大家介紹的android開(kāi)發(fā)高仿課程表的布局實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android實(shí)現(xiàn)EditText換行自動(dòng)縮進(jìn)功能
在很多需要輸入多行文本的應(yīng)用(如記事本、編程代碼編輯器、博客編輯器等)中,自動(dòng)縮進(jìn)功能能大大提升用戶的編輯效率與體驗(yàn),本文給大家介紹了Android實(shí)現(xiàn)EditText換行自動(dòng)縮進(jìn)功能,下面提供整合后的完整代碼示例,需要的朋友可以參考下2025-04-04
Android實(shí)現(xiàn)倒計(jì)時(shí)的按鈕效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)倒計(jì)時(shí)的按鈕效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
Android開(kāi)發(fā)應(yīng)用第一步 安裝及配置模擬器Genymotion
這篇文章主要介紹了Android開(kāi)發(fā)應(yīng)用第一步,即安裝及配置模擬器Genymotion,感興趣的小伙伴們可以參考一下2015-12-12
Android仿網(wǎng)易客戶端頂部導(dǎo)航欄效果
這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)易客戶端頂部導(dǎo)航欄效果,幫助大家制作網(wǎng)易客戶端導(dǎo)航欄特效,感興趣的小伙伴們可以參考一下2016-06-06
Android控件之EditView常用屬性及應(yīng)用方法
本篇文章介紹了,Android控件之EditView常用屬性及應(yīng)用方法。需要的朋友參考下2013-04-04
Android progressbar實(shí)現(xiàn)帶底部指示器和文字的進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android progressbar實(shí)現(xiàn)帶底部指示器和文字的進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01
Android關(guān)于SeekBar無(wú)法點(diǎn)擊到最大值問(wèn)題解決方法記錄(推薦)
這篇文章主要介紹了Android關(guān)于SeekBar無(wú)法點(diǎn)擊到最大值問(wèn)題解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Android中使用GridView進(jìn)行應(yīng)用程序UI布局的教程
GridView即平常我們見(jiàn)到的類似九宮格的矩陣型布局,只不過(guò)默認(rèn)不帶分割線,這里我們就從基礎(chǔ)開(kāi)始來(lái)看一下Android中使用GridView進(jìn)行應(yīng)用程序UI布局的教程2016-06-06

