Holo Light主题的DatePickerDialog?

10

如何在Holo Light主题下获取一个DatePickerDialog?

创建DatePickerDialog时:

 DatePickerDialog dpd = new DatePickerDialog(new ContextThemeWrapper(this,
                    android.R.style.Theme_Holo_Light_Dialog_NoActionBar), 
    new DateListener(v), mTime.year, mTime.month, mTime.monthDay);

如果我使用主题 android.R.style.Theme_Holo_Light 或者 android.R.style.Theme_Holo_Light_Dialog,我可以得到一个具有标准标题和标准按钮的日期选择器。我尝试过使用 Holo light 父级自定义主题,但也没有成功。似乎使用主题 android.R.style.Theme_Holo 可以工作,但结果是一个暗色背景(预期),而我想要一个浅色的背景。

应用程序的 android.jar 版本为 14,应用程序在 Android 版本 3.2 的设备上运行。

我在这里看到了一个示例: http://as400samplecode.blogspot.com/2011/10/android-datepickerdialog.html,该示例展示了一个带有 Holo light 主题的 DatePickerDialog,就像我想要的那样。我不知道为什么在我的设置中它不能工作。

感谢您的帮助。


在manifest.xml文件中的activity标签中加入android:theme="@android:style/Theme.Holo.Light"。 - Padma Kumar
1
@PadmaKumar谢谢您的评论。我将尝试您建议的方法。但通常,我想为活动使用一个具有父级android:Theme.Holo.Light.DialogWhenLarge的主题。 - Julia
@PadmaKumar 我在manifest.xml文件中的activity标签中加入了android:theme="@android:style/Theme.Holo.Light",但似乎没有任何变化(我想是因为我使用了'ContextThemeWrapper')。 - Julia
@Julia:你找到解决方案了吗? - Adrian Grigore
@AdrianGrigore: 不好意思,我没有。 - Julia
1
@Julia,我不确定这里的问题在哪里。我使用android.R.style.Theme_Holoandroid.R.style.Theme_Holo_Light测试了您的代码,两者都给出了相同的对话框和按钮,唯一的区别是深色/浅色风格。那么问题似乎出在哪里呢?也许您应该更新您的问题并附上截图? - rfgamaral
3个回答

18

DatePickerDialog有一个接受主题的构造函数。

DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)

只需更新您的代码以包含所需的样式,无需使用ContextThemeWrapper

DatePickerDialog dpd = new DatePickerDialog(this,
                android.R.style.Theme_Holo_Light_Dialog_NoActionBar, 
new DateListener(v), mTime.year, mTime.month, mTime.monthDay);

以这种方式对我有效。


android.R.style.Theme_Holo_Light_Dialog_NoActionBar 是否适用于版本 < 2.0? - An-droid
@Yume117:不,Holo主题是在Honeycomb 3.0中添加的。 - SuperShalabi

3

阅读这个Android DatePickerDialog示例代码,其中包括如何在DatePickerDialog中使用不同的主题

这里有一个类似的示例

支持定义主题的DatePickerDialog构造函数。

final Calendar c = Calendar.getInstance();  
    int year = c.get(Calendar.YEAR);  
    int month = c.get(Calendar.MONTH);  
    int day = c.get(Calendar.DAY_OF_MONTH);  
return new DatePickerDialog(getActivity(), AlertDialog.THEME_HOLO_DARK, this, year, month, day); 

虽然这个答案可能是正确的,但最好加上一些解释。如果提供的链接失效了,答案就没有意义了。 - Wouter

3

对于Material风格我做了如下修改:

int datePickerThemeResId = 0;
if (android.os.Build.VERSION.SDK_INT >= 21) {
    datePickerThemeResId = android.R.style.Theme_Material_Light_Dialog;
}
new DatePickerDialog(
    context,
    datePickerThemeResId,
    (view, year, month, dayOfMonth) -> {},
    year,
    month,
    day
).show();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接