Android solves DatePicker and Timepicker with layout style issues caused by

Source: Internet
Author: User

Recently need to use Datepicke and timepicker together, but found a problem, through the XML layout file cannot jump size style, XML file content is as follows

<?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:o rientation= "Horizontal" >    <datepicker        android:id= "@+id/date_picker"        android:layout_width= " Wrap_content "        android:layout_height=" wrap_content "        android:layout_gravity=" center_vertical        " Android:calendarviewshown= "false"/>    <timepicker        android:id= "@+id/time_picker"        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:layout_gravity= "Center_ Vertical "/></linearlayout>


Screen Big mobile phone can display full, but the small is so, finally through the access to data to solve the problem, the solution is as follows:

/** * Adjust framelayout size * @param tp */private void Resizepikcer (framelayout tp) {list<numberpicker> nplist = FindNumber Picker (TP); for (Numberpicker np:nplist) {Resizenumberpicker (NP);}} /** * Get viewgroup inside the Numberpicker component * @param viewgroup * @return */private list<numberpicker> findnumberpicker ( ViewGroup viewgroup) {list<numberpicker> nplist = new arraylist<numberpicker> (); View child = Null;if (null! = ViewGroup) {for (int i = 0; i < Viewgroup.getchildcount (); i++) {child = Viewgroup.getchildat (i); if (child instanceof Numberpicker) {Nplist.add ((numberpicker) child);} else if (child instanceof linearlayout) {list<numberpicker> result = Findnumberpicker ((viewgroup) child); Result.size () >0) {return result;}}} return nplist;} /* * Adjust Numberpicker size */private void Resizenumberpicker (Numberpicker np) {Linearlayout.layoutparams params = new Linearlayout.layoutparams (layoutparams.wrap_content);p arams.setmargins (0, 0); Np.setlayoutparams (params                      );                                                      } 

Then the call is done:

Final DatePicker DatePicker = (DatePicker) View.findviewbyid (r.id.date_picker);          Final Timepicker Timepicker = (timepicker) View.findviewbyid (r.id.time_picker);        Resizepikcer (DatePicker);//adjust DatePicker size resizepikcer (timepicker);//Adjust Timepicker size

Look at the adjustment:



Is it very simple, the text can also be changed, through style, examples are as follows:

http://download.csdn.net/detail/msn465780/8447723

Click to open link

Android solves DatePicker and Timepicker with layout style issues caused by

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.