Original address: http://www.ziliao1.com/Article/Show/1ADD173F9F715CF18F8A04A328CF204B.html
Recently in making a time selector, want to DatePicker and Timepicker put together to use, rogue their size style is Google wrote dead, can't find the corresponding attributes to set, they took a little time to write a use of the demo, run the effect as follows.
First look at the layout
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns: tools= "Http://schemas.android.com/tools" android:layout_width= "Match_parent" android:layout_height= "Wrap_content" tools:context= ". Timeactivity " > <numberpicker android:id= "@+id/date_picker" android:layout_width= " 100dip " android:layout_height=" Match_parent " android:layout_margin= "10dip" / > <TimePicker android:id= "@+id/time_picker" android: Layout_width= "Wrap_content" android:layout_height= "Match_parent"
android:layout_margin= "10dip" /> </LinearLayout>
The left uses a Numberpicker replacement datepicker, only displays the month day week
datepicker = (Numberpicker) Findviewbyid (R.id.date_picker);
datepicker.setminvalue (1);
datepicker.setmaxvalue (365); calendar
= calendar.getinstance ();
calendar.setlenient (TRUE); Calendar tempCal = (Calendar)
Calendar.clone ();
long curtime = tempcal.gettimeinmillis (); int curday = calendar.get (Calendar.DAY_OF_YEAR)
; //here simply use 365 for the number of days of the year
String[] displayedValues = new String[365]; for (int i = 0; i < 365; i++) { tempcal =
add (Curtime, i - curday + 1, calendar.day_of_month); //formatted as Month day week displayedvalues[i] = string.format ("%tb %td
%ta ", tempcal, tempcal, tempcal); }
Datepicker.setdisplayedvalues (displayedvalues); datepicker.setvalue (Calendar.get (Calendar.DAY_OF_YEAR));
The Timepicker on the right uses a 24-hour system, showing hours and minutes of Numberpicker (Timepicker is actually made up of Numberpicker, And Numberpicker contains a EditText control in android4.2)
TP = (timepicker) Findviewbyid (r.id.time_picker);
Set to 24 hours, hide am/pm picker
Tp.setis24hourview (true);
Modify the size of the Timepicker font
setnumberpickertextsize (TP);
Modify the size of the Numberpicker in the Timepicker
resizetimerpicker (TP);
The basic idea for modifying the font size is to first find the Numberpicker control inside the Timepicker control, and then find the EditText control inside, so you can freely set its size, color. From the effect of the picture can be seen, can only set the current font, the previous/Next or the original size, here also need to see the source for its reasons, if someone knows also please tell, thank you.
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); if (Result.size () > 0) {
return result;
} } } }
return nplist; } private EditText Findedittext (NUMBERPICKER&NBSP;NP) { if (NULL&NBSP;!=&NBSP;NP) { for (Int i = 0; i < np.getchildcount (); i++) {
view child = np.getchildat (i); if (child Instanceof edittext) {
return (EditText) child; } }
return null; } private void Setnumberpickertextsize (viewgroup viewgroup) {
list<numberpicker> nplist = findnumberpicker (ViewGroup); if (null != nplist) { for ( Numberpicker np : nplist) { edittext et = findedittext (NP);
Et.setfocusable (FALSE); et.setgravity (
Gravity.center); et.settextsize
(10);
} } }
Modify the size of the Timpepicker, is actually set numberpicker size, because the font size settings are not complete, so this place can not set the numberpicker too small, only slightly reduce its size
private void Resizetimerpicker (Timepicker tp) {list<numberpicker> nplist = findnumberpicker (TP); for (Numberpicker np:nplist) {linearlayout.layoutparams params = new linearlayout.layou
Tparams (layoutparams.wrap_content);
Params.setmargins (10, 0, 10, 0);
Np.setlayoutparams (params); }
}
At this point, showing the day of the week and the time of the picker formed, of course, you can all use Numberpicker to complete, that will be troublesome, because the time spent is not enough depth, there are some deficiencies, sorry, if you see the people have other additions please tell me, thank you.
Finally, add a way to modify the font color of picker
First add the theme style to the Styles.xml file and set all the EditText colors (because Timepicker and DatePicker use the basic controls are edittext)
<style name= "Theme.picker" parent= "Android:Theme.Holo.Light" >
<item name= "Android:edittextstyle" > @style/widget.edittext.white</item>
</style>
<style name= "Widget.EditText.White" parent= "@android: Style/widget.edittext" >
<item name= "Android:textcolor" > @color/red</item>
</style>
Then add the property android:theme= "@style/theme.picker" below activity in the app's androidmanifest.xml file to indicate that the acitivity uses the theme. This picker will show you the color you set.
<activity
Android:name= "Com.example.timewidget.TimeActivity"
Android:label= "@string/app_name"
Android:theme= "@style/theme.picker" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</ACTIVITY>