Is it possible to change the style of an Android L TimePickerDialog?

帅比萌擦擦* 提交于 2019-11-26 20:25:07

问题


I was testing my app on an Android L emulator, and I noticed that the TimePickerDialog has significantly changed to this:

This doesn't fit with the theme of my app, and I wanted to know if it is possible to get the old TimePickerDialog style when running on Android L.


回答1:


You can use the TimePickerDialog constructor with theme parameter to specify the theme.

TimePickerDialog(Context context, 
                 int theme, 
                 TimePickerDialog.OnTimeSetListener callBack, 
                 int hourOfDay, 
                 int minute, 
                 boolean is24HourView)



回答2:


Add android:timePickerMode="spinner" to the XML

    <TimePicker
        android:id="@+id/timePicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:timePickerMode="spinner">
    </TimePicker>

https://developer.android.com/reference/android/R.attr.html#timePickerMode

You can choose between spinner or clock modes. This attribute is only available from API level 21, before that the spinner mode was the only option available.




回答3:


If you want the AM / PM to be a picker instead of a button then I recommend you use the single parameter constructor with a ContextThemeWrapper:

new TimePicker(new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light_Dialog_NoActionBar));

If you pass the theme into the multiple parameter constructor then you'll end up with a rather ugly AM / PM button, and the colon will be missing between the hour and minute columns.

new TimePicker(getActivity(), null, TimePickerDialog.THEME_HOLO_LIGHT);




回答4:


As LordRaydenMK said, use the TimePickerDialog constructor with theme parameter and provide this theme: android.R.style.CustomDatePickerDialog

And in your Styles.xml define this:

  <style name="CustomDatePickerDialog"
         parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:dialogTheme">@style/MyDialogTheme</item>
    <item name="android:timePickerStyle">@style/MyDatePicker</item>
  </style>

  <style name="MyDialogTheme"
         parent="Theme.AppCompat.Light.Dialog">
    <item name="android:timePickerStyle">@style/MyDatePicker</item>
  </style>

  <style name="MyDatePicker"
         parent="@android:style/Widget.Material.Light.TimePicker">
    <item name="android:timePickerMode">spinner</item>
  </style>


来源:https://stackoverflow.com/questions/24449723/is-it-possible-to-change-the-style-of-an-android-l-timepickerdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!