Android uses GridLayout to draw custom calendar controls _android

Source: Internet
Author: User
Tags event listener getdate stub visibility

Effect chart

train of thought: is to set the number of GridLayout first, and then put a certain number of custom Calendar button control, the last implementation of the calendar logic can be.

Steps:

First step: Customize the Calendar control (Preliminary)

Step Two: Implement custom single Date button controls

Step three: Add the control from the second step dynamically to the layout of the first step and implement the date logic

Fourth step: Write a single date click the Listener interface

First step: Customize the Calendar control (Preliminary)

<?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" &

  Gt <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:orientation= "Vertical" > <linearlayout android:layout_width= "match_parent" android:layout_height= "55DP" a ndroid:orientation= "vertical" android:background= "@color/lightgreen" > <relativelayout android: Layout_width= "Match_parent" android:layout_height= "match_parent" > <textview android:id= " @+id/textview1 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "and Roid:layout_centerhorizontal= ' true ' android:layout_centervertical= ' true ' android:textcolor= ' @color/whi TE "Android:textsiZe= "9pt" android:text= "October 2016"/> <imagebutton android:id= "@+id/imagebutton1"
          Android:layout_width= "30DP" android:layout_height= "20DP" android:layout_alignparentleft= "true" Android:layout_centervertical= "true" android:layout_marginleft= "16DP" android:background= "@drawa
          Ble/back "/> <imagebutton android:id=" @+id/imagebutton2 "android:layout_width=" 20DP " android:layout_height= "20DP" android:layout_aligntop= "@+id/imagebutton1" Android:layout_center Vertical= "true" android:layout_marginright= "16DP" android:layout_toleftof= "@+id/textview1" a ndroid:background= "@drawable/pre"/> <imagebutton android:id= "@+id/imagebutton3" Androi D:layout_width= "18DP" android:layout_height= "18DP" android:layout_centervertical= "true" Andr Oid:layout_aligntop= "@+iD/textview1 "android:layout_marginleft=" 16DP "android:layout_torightof=" @+id/textview1 "Andr
      oid:background= "@drawable/back11"/> </RelativeLayout> </LinearLayout> <linearlayout
      Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "vertical" android:layout_margintop= "10DP" > <gridlayout android:id= "@+id/gridlayout01" Android:layo Ut_width= "Match_parent" android:layout_height= "Match_parent" > <--! Custom Individual Calendar button controls are placed here--> </ gridlayout> </LinearLayout> <listview android:id= "@+id/listview1" android:layout_width= " Match_parent "android:layout_height=" wrap_content "> </ListView> </LinearLayout> </linearl

 Ayout>
Package com. package name. Mycalendarview;
Import Java.util.Calendar;

Import Java.util.Date;
Import Android.content.Context;
Import Android.graphics.Color;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.WindowManager;
Import Android.widget.GridLayout;
Import Android.widget.ImageButton;
Import Android.widget.LinearLayout;

Import Android.widget.TextView;
Import Com.xuy849.utils.Info;

Import COM.XUY849.WEIGHTAPP.R;
 public class MyCalendar extends LinearLayout implements onclicklistener{context;
 TextView Tv_yearandmonth;
 ImageButton Ib_pre;
 ImageButton Ib_next;
 ImageButton Ib_back;
 GridLayout Gl_calendar;
 View view;
 Calendarbutton buttons[];
 int sidelength;
 String week[] = {"Day", "one", "two", "three", "four", "five", "six"};
 Date date;
 Calendar Calendar;
 int year,month,day;
 
 int res; Public MyCalendar (context context, AttributeSet attrs, int defstyle) {Super(Context, attrs, Defstyle);
 This.context = context;
 TODO auto-generated Constructor stub init ();
 
 Public MyCalendar (context, AttributeSet attrs) {Super (context, attrs);
 This.context = context;
 TODO auto-generated Constructor stub init ();
 
 Public MyCalendar {Super (context);
 This.context = context;
 TODO auto-generated Constructor stub init ();
 
 
 private void Init () {This.view = Layoutinflater.from (context). Inflate (R.layout.my_calendar_view, this);

 }
}

Effect Picture: (Please disregard ListView)

Step Two: Customize a single date button control

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Wrap_content "android:layout_height=" wrap_content "android:background=" @drawable Background01 "android:orientation=" vertical "> <linearlayout android:layout_width=" wrap_content "Androi"
    D:id= "@+id/linearlayout01" android:layout_height= "wrap_content" android:background= "@color/calendarbackground" android:orientation= "vertical" android:layout_marginbottom= "1DP" > <framelayout android:layout_width
        = "Match_parent" android:layout_height= "match_parent" > <imageview android:id= "@+id/imageview1" Android:layout_width= "Match_parent" android:layout_height= "match_parent" android:visibility= "Invis" Ible "android:src=" @drawable/selected_date "/> <linearlayout android:layout_width=" Match_pare NT "android:layout_height=" MaTch_parent "android:orientation=" vertical "android:gravity=" center "> <textview A
          Ndroid:id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:textsize= "8pt" android:text= "/> <textview android:id=" @+id/textview 2 "android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:layout_m argintop= "3DP" android:textcolor= "@color/blue" android:visibility= "Invisible" android:text= "

 65.1 "/> </LinearLayout> </FrameLayout> </LinearLayout> </LinearLayout>

Package com. package name. Mycalendarview;

Import Java.util.Date;
Import Android.content.Context;
Import Android.graphics.Color;
Import Android.util.AttributeSet;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.ImageView;
Import Android.widget.LinearLayout;

Import Android.widget.TextView;
Import Com.xuy849.utils.Info;


Import COM.XUY849.WEIGHTAPP.R;
 Class Calendarbutton extends linearlayout{context;
 TextView tv_date;
 TextView Tv_data;
 ImageView Iv_note;
 LinearLayout Ll_container;
 View view;
 
 Date date;
 
 Public Calendarbutton (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
 This.context = context;
 TODO auto-generated Constructor stub init ();
 
 Public Calendarbutton (context, AttributeSet attrs) {Super (context, attrs);
 This.context = context;
 TODO auto-generated Constructor stub init ();
 } public void Settotalenable (Boolean b) {this.setenabled (b); } public CalendarbUtton (context) {super (context);
 This.context = context;
 
 TODO auto-generated Constructor stub init ();
 public void setdate (date date) {this.date = date;
 Public Date GetDate () {return this.date;
 
 private void init () {view = Layoutinflater.from (context). Inflate (R.layout.date_button, this);
 Findviewbyid tv_date = (TextView) View.findviewbyid (R.ID.TEXTVIEW1);
 Tv_data = (TextView) View.findviewbyid (R.ID.TEXTVIEW2);
 Iv_note = (ImageView) View.findviewbyid (R.ID.IMAGEVIEW1);
 
 Ll_container = (linearlayout) View.findviewbyid (r.id.linearlayout01);
 public void Setdatetext (String text) {tv_date.settext (text);
 public void Setdatetextcolor (int color) {tv_date.settextcolor (color);

 }
 }
 
}

Effect Chart:

Step Three: Add the control in the second step to the layout of the first step in Java code and add the relevant logic

Package com. package name. Mycalendarview;
Import Java.util.Calendar;

Import Java.util.Date;
Import Android.content.Context;
Import Android.graphics.Color;
Import Android.util.AttributeSet;
Import Android.util.Log;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.WindowManager;
Import Android.widget.GridLayout;
Import Android.widget.ImageButton;
Import Android.widget.LinearLayout;

Import Android.widget.TextView;
Import Com.xuy849.utils.Info;

Import COM.XUY849.WEIGHTAPP.R;
 public class MyCalendar extends LinearLayout implements onclicklistener{context;
 TextView Tv_yearandmonth;
 ImageButton Ib_pre;
 ImageButton Ib_next;
 ImageButton Ib_back;
 GridLayout Gl_calendar;
 View view;
 Calendarbutton buttons[];
 int sidelength;
 String week[] = {"Day", "one", "two", "three", "four", "five", "six"};
 Date date;
 Calendar Calendar;
 int year,month,day;
 
 int res; Public MyCalendar (context context, AttributeSet attrs, int defstyle) {Super(Context, attrs, Defstyle);
 This.context = context;
 TODO auto-generated Constructor stub init ();
 
 Public MyCalendar (context, AttributeSet attrs) {Super (context, attrs);
 This.context = context;
 TODO auto-generated Constructor stub init ();
 
 Public MyCalendar {Super (context);
 This.context = context;
 TODO auto-generated Constructor stub init ();
 
 private void Init () {This.view = Layoutinflater.from (context). Inflate (R.layout.my_calendar_view, this);
 WindowManager ww = (WindowManager) getcontext (). Getsystemservice (Context.window_service);
 
 Sidelength = Ww.getdefaultdisplay (). GetWidth ()/info.column_count;
 
 Myfindviewbyid ();
 
 Initialize Calendar button layout initcalendarlayout ();
 
 Initialize button click event Initbutton ();
 private void Initbutton () {Ib_pre.setonclicklistener (this);
 Ib_next.setonclicklistener (this);
 Ib_back.setonclicklistener (this); private void Myfindviewbyid () {tv_yearandmonth = (TextView) View.findviewbyid (r.id. TextView1);
 Ib_pre = (ImageButton) View.findviewbyid (R.id.imagebutton2);
 Ib_next = (ImageButton) View.findviewbyid (R.id.imagebutton3);
 Ib_back = (ImageButton) View.findviewbyid (R.id.imagebutton1);
 
 Gl_calendar = (GridLayout) View.findviewbyid (r.id.gridlayout01);
 
 buttons = new Calendarbutton[info.column_count*info.row_count]; 
 ////According to the calendar passed over, draw the month's calendars view private void Initcalendar (Calendar calendar) {int year = Calendar.get (calendar.year);
 int month = (Calendar.get (calendar.month) +1);
 
 int date = Calendar.get (calendar.date);
 Set title String Todaystr = String.Format ("%04d year%02d Month", Calendar.get (Calendar.year), (Calendar.get (Calendar.month) +1));
 
 Tv_yearandmonth.settext (TODAYSTR);
 Calendar.set (Calendar.day_of_month, 1);
 int currentmonthfirstdateinweek = Calendar.get (Calendar.day_of_week)-1;
 Calendar.set (calendar.day_of_month, date);
 
 int currentmonthdayssum = Calendar.getactualmaximum (calendar.day_of_month);
 Calendar.roll (Calendar.month,-1); int LastmonthdaysSum = Calendar.getactualmaximum (calendar.day_of_month);
 int i;
 
 Calendar.roll (Calendar.month, 1);
 LOG.W ("Month", currentmonthdayssum+ "Day");
 
 LOG.W ("Last month", Lastmonthdayssum + "days");
 * * Set Date///set LOG.W for this month ("Currentmonthfirstdateinweek", currentmonthfirstdateinweek+ "");
 
 LOG.W ("currentmonthfirstdateinweek%7111", currentmonthdayssum+currentmonthfirstdateinweek%7+ ""); For (i=currentmonthfirstdateinweek%7+7;i<= (currentmonthdayssum+currentmonthfirstdateinweek%7+7-1) &&i
  <info.column_count*info.row_count;i++) {buttons[i].changetostate (info.state_normal_norecored);
  Buttons[i].setdatetext ((i-currentmonthfirstdateinweek%7-7+1) + "");
  Buttons[i].setdatetextcolor (Color.Black);
  Buttons[i].setenabled (TRUE);
 Buttons[i].setdate (New Date (year,month,i-(currentmonthfirstdateinweek%7+7) +1));
  }//set for one months for (i=7;i< (currentmonthfirstdateinweek%7+7) &&i<info.column_count*info.row_count;i++) {
  Buttons[i].changetostate (info.state_normal_norecored); Buttons[i].setdaTetextcolor (Color.gray);
  Buttons[i].setdatetext (lastmonthdayssum-(currentmonthfirstdateinweek%7-i%7) +1+ "");
 Buttons[i].setenabled (FALSE);
  //Set one month for (i =currentmonthdayssum+currentmonthfirstdateinweek%7+7;i<info.row_count*info.column_count;i++) {
  Buttons[i].changetostate (info.state_normal_norecored);
  Buttons[i].setdatetextcolor (Color.gray);
  Buttons[i].setdatetext ((I-(currentmonthdayssum+currentmonthfirstdateinweek%7+7) +1) + "");
 Buttons[i].setenabled (FALSE); }//Set the day if (year==this.year&&month==this.month&&day==this.day) buttons[date+
 
 Currentmonthfirstdateinweek%7+7-1].changetostate (info.state_today_norecored);
 
 Initialize interface if (res!=0) initlayout (res);
 private void Initcalendarlayout () {//Set number of rows, columns Gl_calendar.setrowcount (info.row_count);
 
 
 Gl_calendar.setcolumncount (Info.column_count);
 * * Add button to layout/int i;
 
 int sum = Info.row_count*info.column_count; Set week for (i=0;i<info.column_count;i++) {buttons[i] = new CalendaRbutton (context);
  Buttons[i].setdatetext (Week[i]);
  Buttons[i].setlayoutparams (New Layoutparams (Sidelength, sidelength));
  Gl_calendar.addview (Buttons[i], i);
 Buttons[i].setenabled (FALSE);
  for (i = info.column_count;i<sum;i++) {Buttons[i] = new Calendarbutton (context);
  Buttons[i].setdatetext ("55");
  Buttons[i].setlayoutparams (New Layoutparams (Sidelength, sidelength));
 Gl_calendar.addview (Buttons[i], i);
 Calendar = calendar.getinstance () According to the month setting;
 Year = Calendar.get (calendar.year);
 month = Calendar.get (calendar.month) +1;
 Day = Calendar.get (calendar.date);
 
 
 Initcalendar (Calendar); @Override public void OnClick (View v) {//TODO auto-generated Method Stub switch (V.getid ()) {//Previous one-month case r.id.
  imagebutton2:{Calendar.roll (Calendar.month,-1);
  Initcalendar (Calendar);
 Break
  }//Next one months case r.id.imagebutton3:{calendar.roll (calendar.month, 1);
  Initcalendar (Calendar);
 Break }} public void Setonclickbuttonlistener (ONCLICklistener L,int index) {buttons[index].setonclicklistener (L);
 Public Date getDate (int index) {return buttons[index].getdate ();
 public void SetData (float f,int index) {buttons[index].tv_data.settext (String.Format ("%.1f", f));
 Buttons[index].tv_data.setvisibility (textview.visible);
 public void Initlayout (int res) {switch (res) {case info.view_weiht:{break;
 } default:{break;
 }} public void Setres (int res) {this.res = res;


 }
}

Fourth Step: Write a single date click the Listener interface

Add a method in step three:

Set subscript is Index date button click event Listener public
void Setonclickbuttonlistener (Onclicklistener l,int index) { 
 Buttons[index ].setonclicklistener (l);
 }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.