java 如何更改 Android 的 Datepicker 样式

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

How to Change the Datepicker style for Android

javaandroidxmlandroid-datepicker

提问by JulianMartinez23

I'm currently making a registration form, and one of the fields is for the users Date of Birth. I want to use datepicker, however I don't want the calendar layout as shown below:

我目前正在制作注册表,其中一个字段是用户的出生日期。我想使用日期选择器,但是我不想要如下所示的日历布局:

enter image description here

在此处输入图片说明

I want the layout to look something like this so that way its easier to choose the year and month without having to scroll through everything:

我希望布局看起来像这样,这样可以更轻松地选择年份和月份,而无需滚动浏览所有内容:

enter image description here

在此处输入图片说明

However I do not know how to go about this, or what to put inside the styles.xml file. Any help would be greatly appreciated.

但是我不知道该怎么做,或者在styles.xml 文件中放什么。任何帮助将不胜感激。

回答by Victor V.

Add this to style.xml

将此添加到 style.xml

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

And then you should apply style for your date picker like this:

然后你应该像这样为你的日期选择器应用样式:

<DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/datePicker"
        style="@style/date_picker_theme" />

回答by yogikalwar

Create style with

创建样式

<style name="DatePicker" parent="@android:style/Theme.Holo.Light">
    <item name="android:datePickerStyle">@android:style/Widget.DatePicker</item>
</style>

Then in custom fragment dialog xml

然后在自定义片段对话框 xml

<DatePicker
    android:theme="@style/DatePicker"
    android:id="@+id/fragment_date_picker_control"
    android:datePickerMode="spinner"
    android:spinnersShown="true"
    android:calendarViewShown="false"
    android:layout_marginLeft="@dimen/margin_normal"
    android:layout_marginRight="@dimen/margin_normal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" />

Link : http://yogeshkalwar.blogspot.in/2017/08/custom-spinner-datepicker-supporting-n70.html

链接:http: //yogeshkalwar.blogspot.in/2017/08/custom-spinner-datepicker-supporting-n70.html

回答by Bj?rn Egil

To set the DatePicker style globally, add the following to style.xml, or adjust existing content:

要全局设置 DatePicker 样式,请将以下style.xml内容添加到,或调整现有内容:

<resources>
  <style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    <item name="android:datePickerStyle">@style/MyDatePicker</item>
  </style>
  ...
</resources>

And define your customized DatePicker style, either in style.xmlor a separate file, e.g.:

并在style.xml或单独的文件中定义您自定义的 DatePicker 样式,例如:

<resources>
  <style name="MyDatePicker" parent="@android:style/Widget.DatePicker">
    <item name="android:calendarViewShown">false</item>
    <item name="android:datePickerMode">spinner</item>
  </style>
</resources>

回答by Anshul Verma

Above properties are of XML, if you want to set it programmatically you can you the below code but I haven't checked this code whether its working or not.

上面的属性是 XML 的,如果你想以编程方式设置它,你可以使用下面的代码,但我还没有检查过这段代码是否有效。

datepickerDialog_object.getDatePicker().setSpinnersShown(true);
datepickerDialog_object.getDatePicker().setCalendarViewShown(false);

回答by Mantas Laurinavi?ius

DatePickerDialog has a convenience method, where you set the desired layout.

DatePickerDialog 有一个方便的方法,您可以在其中设置所需的布局。

public DatePickerDialog(@NonNull Context context, @StyleRes int themeResId,
        @Nullable OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth) {
    this(context, themeResId, listener, null, year, monthOfYear, dayOfMonth);
}


public static final int MODE_SPINNER = 1;
public static final int MODE_CALENDAR = 2;

Passing 1 or 2 in themesResId does the trick.

在 themesResId 中传递 1 或 2 可以解决问题。

回答by Muhammad Faisal

It might be too late but you can set it programmatically like these.
Apply it inside your onClicklistener.

可能为时已晚,但您可以像这样以编程方式设置它。
将其应用到您的onClick听众中。

        DatePickerDialog datePicker = new DatePickerDialog(
                this,
                android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                datePickerListener,
                cal.get(Calendar.YEAR),
                cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH));
        datePicker.setCancelable(false);
        datePicker.setTitle("Select the date");
        datePicker.show();

The android.R.style.Theme_Holo_Light_Dialog_MinWidthcreate style that you want.
Hope it helps someone.

android.R.style.Theme_Holo_Light_Dialog_MinWidth您想要的创建样式。
希望它可以帮助某人。

回答by Muhammad Haroon

hi try this a simplest way...

嗨,试试这个最简单的方法......

public void datePicker(final Context context, final EditText editText, final String type) {
        Calendar calendar = Calendar.getInstance();
        final SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                newDate.set(year, monthOfYear, dayOfMonth);
                editText.setText(dateFormatter.format(newDate.getTime())

            }

        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
//        datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
        datePickerDialog.show();
    }

android in style.xml add below line

android 在 style.xml 添加下面一行

 <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorAccent">@color/AppDarkBlue</item>
    </style>