是否可以更改 Android L TimePickerDialog 的样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24449723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:10:47  来源:igfitidea点击:

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

androidandroid-timepickerandroid-5.0-lollipop

提问by JDJ

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

我在 Android L 模拟器上测试我的应用程序,我注意到 TimePickerDialog 已显着更改为:

Android L TimePickerDialog

Android L TimePickerDialog

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.

这不符合我的应用程序的主题,我想知道在 Android L 上运行时是否可以获取旧的 TimePickerDialog 样式。

采纳答案by LordRaydenMK

You can use the TimePickerDialogconstructor with themeparameter to specify the theme.

您可以使用带参数的TimePickerDialog构造theme函数来指定主题。

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

回答by yehyatt

Add android:timePickerMode="spinner"to the XML

android:timePickerMode="spinner" 添加到 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

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

You can choose between spinneror clockmodes. This attribute is only available from API level 21, before that the spinnermode was the only option available.

您可以在微调器时钟模式之间进行选择。此属性仅在 API 级别 21 中可用,在此之前微调模式是唯一可用的选项。

回答by Dan J

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:

如果您希望 AM / PM 成为选择器而不是按钮,那么我建议您使用带有 的单参数构造函数ContextThemeWrapper

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

AM-PM picker

AM-PM 选择器

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.

如果您将主题传递给多参数构造函数,那么您最终会得到一个相当丑陋的 AM / PM 按钮,并且小时和分钟列之间将缺少冒号。

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

AM-PM button

上午-下午按钮

回答by David Riha

As LordRaydenMK said, use the TimePickerDialogconstructor with themeparameter and provide this theme: android.R.style.CustomDatePickerDialog

正如 LordRaydenMK 所说,使用TimePickerDialogtheme参数的构造函数并提供这个主题:android.R.style.CustomDatePickerDialog

And in your Styles.xmldefine this:

在你Styles.xml定义这个:

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