【问题标题】:How to change the style of a DatePicker in android?如何在 android 中更改 DatePicker 的样式?
【发布时间】:2015-07-26 04:55:06
【问题描述】:

我想更改 Android 中日期/时间选择器对话框的默认颜色,使其与我的应用主题相匹配。我在 Google 上搜索了解决方案,但找不到解决方案。

我正在做的是创造一种新的风格:

  <style name="datepicker" parent="@android:style/Widget.DeviceDefault.DatePicker">
    <!---TODO-->
    <item name="android:focusedMonthDateColor">@android:color/holo_red_dark</item>
</style>

不知道日期选择器对话的可用属性是什么。如果有人可以在上面发布链接,那就太好了

添加样式后,我在我的主要样式中将其称为

 <item name="android:datePickerStyle">@style/datepicker</item>

不幸的是,这对我根本不起作用。

【问题讨论】:

    标签: android android-styles android-datepicker android-timepicker


    【解决方案1】:

    这样打电话

     button5.setOnClickListener(new View.OnClickListener() {
    
     @Override
     public void onClick(View v) {
     // TODO Auto-generated method stub
    
     DialogFragment dialogfragment = new DatePickerDialogTheme();
    
     dialogfragment.show(getFragmentManager(), "Theme");
    
     }
     });
    
    public static class DatePickerDialogTheme extends DialogFragment implements DatePickerDialog.OnDateSetListener{
    
         @Override
         public Dialog onCreateDialog(Bundle savedInstanceState){
         final Calendar calendar = Calendar.getInstance();
         int year = calendar.get(Calendar.YEAR);
         int month = calendar.get(Calendar.MONTH);
         int day = calendar.get(Calendar.DAY_OF_MONTH);
    
    //for one
    
    //for two 
    
     DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
     AlertDialog.THEME_DEVICE_DEFAULT_DARK,this,year,month,day);
    
    //for three 
     DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
     AlertDialog.THEME_DEVICE_DEFAULT_LIGHT,this,year,month,day);
    
    // for four
    
       DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
     AlertDialog.THEME_HOLO_DARK,this,year,month,day);
    
    //for five
    
     DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
         AlertDialog.THEME_HOLO_LIGHT,this,year,month,day);
    
    //for six
    
     DatePickerDialog datepickerdialog = new DatePickerDialog(getActivity(),
     AlertDialog.THEME_TRADITIONAL,this,year,month,day);
    
    
         return datepickerdialog;
         }
    
         public void onDateSet(DatePicker view, int year, int month, int day){
    
         TextView textview = (TextView)getActivity().findViewById(R.id.textView1);
    
         textview.setText(day + ":" + (month+1) + ":" + year);
    
         }
         }
    

    按照这个它会给你所有类型的日期选择器样式(从这里复制)

    http://www.android-examples.com/change-datepickerdialog-theme-in-android-using-dialogfragment/

    【讨论】:

    • @WolfJee 这样使用;诠释主题; if (Build.VERSION.SDK_INT
    • 非常好的答案,如果您不希望仅为 datePicker 更改应用程序的样式和主题。但是您刚刚弄乱了示例图像的编号,例如 .THEME_DEVICE_DEFAULT_LIGHT 对应于我认为的第二张图像。此外,对于 API >23 的上述代码,我现在也遇到了已弃用的错误
    • 我讨厌谷歌日期选择器的新“材料”样式,因为用户不知道点击左上角可以更改年份!糟糕的用户体验!
    • @kaya 您的建议无效。在 Android 7.0 模拟器上试用
    • @WolfJee 你找到解决方案了吗?
    【解决方案2】:

    试试这个。这是最简单、最有效的方法

    <style name="datepicker" parent="Theme.AppCompat.Light.Dialog">
            <item name="colorPrimary">@color/primary</item>
            <item name="colorPrimaryDark">@color/primary_dark</item>
            <item name="colorAccent">@color/primary</item>
    </style>
    

    【讨论】:

    • 如何设置按钮样式?我尝试设置 buttonBarNegativeButtonStyle buttonBarNeutralButtonStyle buttonBarPositiveButtonStyle buttonStyle 但没有反映。
    • 我有同样的问题,无法更改按钮样式
    • 对 Kotlin 无效。
    【解决方案3】:

    要在应用程序级别更改 DatePicker 颜色(日历模式),请定义以下属性。

    <style name="MyAppTheme" parent="Theme.AppCompat.Light">
        <item name="colorAccent">#ff6d00</item>
        <item name="colorControlActivated">#33691e</item>
        <item name="android:selectableItemBackgroundBorderless">@color/colorPrimaryDark</item>
        <item name="colorControlHighlight">#d50000</item>
    </style>
    

    查看http://www.zoftino.com/android-datepicker-example 了解其他DatePicker 自定义样式

    【讨论】:

    • 如何更改按钮文本颜色?
    • @Mahdi 看到 Rohit 的回答(这将改变按钮文本颜色)
    【解决方案4】:

    创建新样式

    <style name="my_dialog_theme" parent="ThemeOverlay.AppCompat.Dialog">
        <item name="colorAccent">@color/colorAccent</item>                   <!--header background-->
        <item name="android:windowBackground">@color/colorPrimary</item>     <!--calendar background-->
        <item name="android:colorControlActivated">@color/colorAccent</item> <!--selected day-->
        <item name="android:textColorPrimary">@color/colorPrimaryText</item> <!--days of the month-->
        <item name="android:textColorSecondary">@color/colorAccent</item>    <!--days of the week-->
    </style>
    

    然后初始化对话框

    Calendar mCalendar = new GregorianCalendar();
    mCalendar.setTime(new Date());
    
    new DatePickerDialog(mContext, R.style.my_dialog_theme, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            //do something with the date
        }
    }, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH)).show();
    

    结果:

    【讨论】:

    • 如何更改按钮样式或按钮文本颜色?
    • @Mahdi 看到 Rohit 的回答(这将改变按钮文本颜色)
    • 如果在xml中使用DatePicker会怎样?
    【解决方案5】:
    Calendar calendar = Calendar.getInstance();
    
    DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), R.style.DatePickerDialogTheme, new DatePickerDialog.OnDateSetListener() {
      public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Calendar newDate = Calendar.getInstance();
        newDate.set(year, monthOfYear, dayOfMonth);
    
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
        String date = simpleDateFormat.format(newDate.getTime());
      }
    }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
    
    datePickerDialog.show();
    

    并使用这种风格:

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

    【讨论】:

    • 如果在xml中使用DatePicker会怎样?
    【解决方案6】:

    (我正在使用 React Native;targetSdkVersion 22)。我正在尝试更改日历日期选择器对话框的外观。接受的答案对我不起作用,但确实如此。希望这个 sn-p 对你们中的一些人有所帮助。

    <style name="CalendarDatePickerDialog"  parent="Theme.AppCompat.Light.Dialog">
        <item name="colorAccent">#6bf442</item>
        <item name="android:textColorPrimary">#6bf442</item>
    </style>
    

    【讨论】:

    • 不是和接受的答案一样的代码 sn-p 吗?
    • 差不多,只是名字不一样。将名称设置为“datepicker”对我不起作用,但将其更改为“CalendarDatePickerDialog”使其工作。
    【解决方案7】:

    由于AlertDialog.THEME 属性已被弃用,在创建DatePickerDialog 时,您应该为int themeResId 传递这些参数之一

    • android.R.style.Theme_DeviceDefault_Dialog_Alert
    • android.R.style.Theme_DeviceDefault_Light_Dialog_Alert
    • android.R.style.Theme_Material_Light_Dialog_Alert
    • android.R.style.Theme_Material_Dialog_Alert

    【讨论】:

      【解决方案8】:

      创建自定义DatePickerDialog 样式:

      <style name="AppTheme.DatePickerDialog" parent="Theme.MaterialComponents.Light.Dialog">
          <item name="android:colorAccent">@color/colorPrimary</item>
          <item name="android:colorControlActivated">@color/colorPrimaryDark</item>
          <item name="android:buttonBarPositiveButtonStyle">@style/AppTheme.Alert.Button.Positive</item>
          <item name="android:buttonBarNegativeButtonStyle">@style/AppTheme.Alert.Button.Negative</item>
          <item name="android:buttonBarNeutralButtonStyle">@style/AppTheme.Alert.Button.Neutral</item>
      </style>
      
      <style name="AppTheme.Alert.Button.Positive" parent="Widget.MaterialComponents.Button.TextButton">
          <item name="android:textColor">@color/buttonPositive</item>
      </style>
      
      <style name="AppTheme.Alert.Button.Negative" parent="Widget.MaterialComponents.Button.TextButton">
          <item name="android:textColor">@color/buttonNegative</item>
      </style>
      
      <style name="AppTheme.Alert.Button.Neutral" parent="Widget.MaterialComponents.Button.TextButton">
          <item name="android:textColor">@color/buttonNeutral</item>
      </style>
      

      在应用主题中设置自定义datePickerDialogTheme 样式:

      <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
          <item name="android:datePickerDialogTheme">@style/AppTheme.DatePickerDialog</item>
      </style>
      

      在初始化时以编程方式设置主题,如下所示:

      val datetime = DatePickerDialog(this, R.style.AppTheme_DatePickerDialog)
      

      【讨论】:

      • 谢谢。我以前有隐形/白色的正面和负面按钮。这解决了它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      相关资源
      最近更新 更多