Android development of a highly imitated timetable layout example detailed _android

Source: Internet
Author: User
Tags rand

First of all, this demo, this is a copy of the timetable layout file, although I am a rookie, but I still want to leave some examples of learning, first look at the effect

And then we'll take a look at our school app.

Layout analysis

First, the layout of the partition

First, the entire page is placed below a linearlayout layout, divided into the top and bottom two sections, the following is the detailed information showing the schedule

1: This is nothing to tell, is directly a linearlayout layout, and then the control of a textview to display the year, a view used as a vertical bar, a spinner to display the number of selected weeks

2: This is the part that shows the day of the week, is my custom view, rewrite the OnDraw method myself, then draw seven words, the code is as follows:

public void OnDraw (Canvas Canvas)
{
//Get the width of the current view
int width = getwidth ();
int offset = WIDTH/8;
int currentposition = offset;
Sets the font to be drawn
Mpaint.settypeface (Typeface.create (Typeface.default_bold, Typeface.bold));
Mpaint.settextsize (a);
Mpaint.setcolor (Color.rgb (0, 134, 139));
for (int i = 0; i < 7; i++)
{
//circled the current date
if (day = = i)
{
System.out.println ("Draw the current date!");
}
Canvas.drawtext (Days[i], currentposition, mpaint);
CurrentPosition + = offset
;
Invoke the parent class's drawing method
Super.ondraw (canvas);
}

3: This is also a linearlayout, direct handwriting layout that will be written out, setting the LinearLayout orientation property to vertical.

4: This is a GridView, and then you inherit baseadapter fill the content, the following put the layout of the XML file

<?xml version= "1.0" encoding= "Utf-8"?> <!--mimic the interface of the curriculum--> <linearlayout xmlns:android= "http://
Schemas.android.com/apk/res/android "android:layout_width=" match_parent "android:layout_height=" Match_parent " android:orientation= "Vertical" > <!--display time--> <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"/> <!--vertical line--> < 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"/> <!--dropdown method of selection of weeks--> <spinner android:id= "@+id/switchweek" android:layout_width= match_parent "android:layout_height=" Match_paRent "android:gravity= Center" android:layout_gravity= "center"/> </LinearLayout> <!--separator line--> <view Android:layout_width= "Match_parent" android:layout_height= "1DP" android:background= "#00FF7F"/> <!--Show week-- > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" 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> <!--show Schedule details--
> <scrollview android:layout_width= "match_parent" android:layout_height= "match_parent" > <LinearLayout
Android:layout_width= "Match_parent" android:layout_height= "Match_parent" > <!--show how many lessons--> <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= "One" android:textsize= "10DP" "android:gravity=" center "/> <textview android:layout_width=" Wrap_ Content "android:layout_height=" 92DP "android:textsize=" 12DP "android:text=" two "android:gravity=" Center "/> <
TextView android:layout_width= "wrap_content" android:layout_height= "92DP" android:textsize= "12DP" android:text= "three"
android:gravity= "center"/> <textview android:layout_width= "wrap_content" android:layout_height= "92DP" Android:textsize= "12DP" android:text= "four" android:gravity= "center"/> <textview android:layout_width= "Wrap_" Content "android:layout_height=" 92DP "android:textsize=" 12DP "android:text=" five "android:gravity=" center "/> <
TextView android:layout_width= "wrap_content" android:layout_height= "92DP" android:textsize= "12DP" android:text= "six" 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>

The following is the GridView adapter code:

public class Myadapter extends Baseadapter {private context Mcontext;//Save the contents of the internal array private list<string> content; pu Blic myadapter (context, list<string> List) {this.mcontext = context; this.content = List.} public int Getc Ount () {return content.size ():} public Object getitem (int position) {return content.get (position);} public Long Getite mId (int position) {return position} public View GetView (int position, View Convertview, ViewGroup parent) {if convert
View = = null) {Convertview = Layoutinflater.from (Mcontext). Inflate (R.layout.grib_item, NULL);
TextView TextView = (TextView) Convertview.findviewbyid (R.id.text);
If there is a class, add the data if (!getitem (position). Equals ("")) {Textview.settext ((String) GetItem (position));
Textview.settextcolor (Color.White);
Transform color int rand = position% 7;
Switch (RAND) {Case 0:textview.setbackground (mcontext.getresources (). getdrawable (R.DRAWABLE.GRID_ITEM_BG)); 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; }
}

Let's take a moment to explain, first of all, to customize the adapter must inherit a adapter, where we inherit from Baseadapter, after inheriting must rewrite GetCount (), GetItem (int), getitemid (int), GetView ( Int,view,viewgroup) These methods have to be rewritten, the most important is to rewrite the GetView () method, because this method returns a View, then the GridView is the layout of each child item.

Convertview=layoutinflater.from (Mcontext). Inflate (R.layout.grib_item, NULL);

This line of code is to load the layout file and return a view

 if (!getitem (position). Equals ("")) {Textview.settext ((String) GetItem (position));
Textview.settextcolor (Color.White);
Transform color int rand = position% 7;
Switch (RAND) {Case 0:textview.setbackground (mcontext.getresources (). getdrawable (R.DRAWABLE.GRID_ITEM_BG));
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 }
}

The code here is the background for judging each column and then implementing the Change view, where the code for the background is as follows:

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

The other is similar, and there is the item layout file:

<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>

This is to imitate the layout of the timetable, and finally attach the source git address: point I download

The above is a small set to introduce the Android development of high imitation timetable of the layout example, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.