Android 与活动风格不同的日期选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20835126/
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 03:33:22  来源:igfitidea点击:

datepicker with different style than activity

androidandroid-themeandroid-stylesandroid-datepicker

提问by mehdok

I have activities with custom style, I want my activity with a custom title bar so I created below style:

我有自定义样式的活动,我希望我的活动具有自定义标题栏,所以我创建了以下样式:

<style name="costum_titlebar">
    <item name="android:background">@color/titlebar_background</item>
</style>

<style name="CustomTheme" parent="android:Theme">
    <item name="android:windowTitleSize">@dimen/titlebar_size</item>
    <item name="android:windowTitleBackgroundStyle">@style/costum_titlebar</item>
</style>

In manifest I use:

在清单中我使用:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomTheme" >

In the activity I use:

在我使用的活动中:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.setting_activity_titlebar);

So far it's good and works fine, I have activity with my custom title bar.
Now I want to use datePickerDialog. The problem is that dialog looks old:

到目前为止它很好并且工作正常,我的自定义标题栏有活动。
现在我想使用 datePickerDialog。问题是对话框看起来很旧:

enter image description here

在此处输入图片说明

But I want it to look like this:

但我希望它看起来像这样:

enter image description here

在此处输入图片说明

I also have this style:

我也有这种风格:

<style name="AppBaseTheme" parent="android:Theme.Light">

So I used it to create DatePickerDialog like this:

所以我用它来创建 DatePickerDialog 像这样:

new DatePickerDialog(getActivity(),R.style.AppBaseTheme, this, year, month, day);

But the theme won't apply.

但主题不适用。

Just for a test, I create a second app with default style and just one button to open date picker, so the datepicker Looks like what I want. Both apps have android:minSdkVersion="8"

只是为了测试,我使用默认样式创建了第二个应用程序,只需一个按钮即可打开日期选择器,因此日期选择器看起来像我想要的。两个应用程序都有android:minSdkVersion="8"

I think the problem is with custom titlebar, so how I can get my custom titlebar and datepicker with desired style?

我认为问题出在自定义标题栏上,那么如何获得具有所需样式的自定义标题栏和日期选择器?

EDIT:

编辑:

I Changed android:minSdkVersion="11"
with
<style name="datePickerTheme" parent="@android:style/Widget.Holo.DatePicker"></style>
and
new DatePickerDialog(getActivity(),R.style.datePickerTheme, this, year, month, day);

我改变android:minSdkVersion="11"

<style name="datePickerTheme" parent="@android:style/Widget.Holo.DatePicker"></style>

new DatePickerDialog(getActivity(),R.style.datePickerTheme, this, year, month, day);

But the Result still is the same.

但结果还是一样。

回答by erkinyldz

I know this is little late but I walked into this topic while I was searching something else about DatePickers.

我知道这有点晚了,但我在搜索有关 DatePickers 的其他内容时走进了这个主题。

Add this to the custom theme you are using:

将此添加到您正在使用的自定义主题中:

<item name="android:datePickerStyle">@android:style/Widget.DatePicker</item>

This will override the parent theme you are using and result as a different looking DatePicker.

这将覆盖您正在使用的父主题,并导致外观不同的 DatePicker。

You will need to create the dialog with this custom theme.

您将需要使用此自定义主题创建对话框。

Example of my theme:

我的主题示例:

<style name="CustomTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>
    <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
    <item name="android:actionButtonStyle">@style/MyActionBarButtonStyle</item>
    <item name="android:actionMenuTextColor">#ffffff</item>
    <item name="android:windowBackground">@drawable/bg</item>
    <item name="android:datePickerStyle">@android:style/Widget.DatePicker</item>
</style>

回答by jinal

Try like this:

像这样尝试:

style="@android:style/Widget.DatePicker"

回答by Rana.S

DatePickerDialog dateDialog = new DatePickerDialog(getActivity(),
                                                   android.R.style.Theme_Holo_Light_Dialog,
                                                   ondateSet, year, month, day);
dateDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

This is for showing white color light background Date picker Fragment.

这是用于显示白色浅色背景日期选择器片段。