【问题标题】:Change DatePicker background color更改 DatePicker 背景颜色
【发布时间】:2018-06-29 18:16:45
【问题描述】:

我试图在另一个活动之上显示一个DatePicker 对话框,而正在发生的事情是它以某种方式继承了它的颜色。

我希望它有一个绿色的标题和白色的背景,

这里是样式的摘录

<style name="DatePickerDialog" parent="@android:style/Theme.Holo.Light">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="colorAccent">@color/primary</item>
</style>

而这段代码是用来弹出DatePicker

    DatePickerDialog datepicker = new DatePickerDialog(this, R.style.DatePickerDialog, new DatePickerDialog.OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            TextView newdate = (TextView) findViewById(R.id.newdate);
            Date date = getDate(year, monthOfYear, dayOfMonth);
            DateFormat dateformat = new SimpleDateFormat(getResources().getString(R.string.date_format_full));
            newdate.setText(dateformat.format(date));
        }
    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    datepicker.show();

如果我在样式中指定白色背景,

    <item name="android:background">@color/app_background</item>

我尝试的最后一件事是使用AlertDialog.THEME_DEVICE_DEFAULT_DARK 作为DatePicker 主题

DatePickerDialog datepicker = new DatePickerDialog(this, 
AlertDialog.THEME_DEVICE_DEFAULT_DARK, new 
DatePickerDialog.OnDateSetListener() 

这是我从中打开对话框的活动的样式

<style name="UserDialog" parent="android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:background">@color/primary</item>
    <item name="android:textColor">@color/dialog_text</item>
    <item name="colorPrimary">@color/app_background</item>
    <item name="android:windowTitleStyle">@style/NewDialogTitle</item>
</style>

<style name="NewDialogTitle" parent="@android:style/TextAppearance.DialogWindowTitle">
    <item name="android:gravity">center_horizontal</item>
</style>

以及我使用的颜色

<color name="primary">#4CAF50</color>
<color name="app_background">#FFFFFF</color>

有人知道怎么做吗?我会很感激任何指导。我已经尝试关注this answer, but had no luck

【问题讨论】:

    标签: android datepicker


    【解决方案1】:

    这段代码对我有用,试试这个...

    styles.xml

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

    弹出代码

            Calendar mcurrentDate = Calendar.getInstance();
            int mYear = mcurrentDate.get(Calendar.YEAR);
            int mMonth = mcurrentDate.get(Calendar.MONTH);
            int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
    
            DatePickerDialog mDatePicker;
            mDatePicker = new DatePickerDialog(context, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
    
                    Toast.makeText(context,"Selected Date " + + selectedday + "-" + ++selectedmonth  + "-" + selectedyear ,Toast.LENGTH_SHORT).show();
                }
            }, mYear, mMonth, mDay);
            mDatePicker.show();
    

    【讨论】:

    • 我已尝试按照您的建议运行它,但结果是相同的绿色窗口,这次在所选日期上带有黑色重音
    • 你得到什么颜色?你能详细说明一下吗
    • 从您的活动样式中删除除 colorPrimary、colorPrimaryDark 和 colorAccent 之外的所有内容,然后使用上面的代码它应该可以工作
    • 有用的解决方案:-)
    【解决方案2】:

    要在应用程序级别更改 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>
    

    参考:How to change the style of a DatePicker in android?

    【讨论】:

    • 我尝试将其添加到应用程序主题中,但得到了相同的结果。看起来对话框没有使用它
    【解决方案3】:

    下面的自定义 DatePickerDoalog 类不仅可以自定义暗淡颜色,还可以让暗淡看起来有动画效果

    /**
     * @author Taras Yurkiv @Devlight
     */
    public class DatePickerDialogCustomDim extends DatePickerDialog {
    
        private final long animDuration = 100;
        private float dimAmount = 0.7f;
    
        private Drawable dimDrawable;
        private ViewGroup root;
    
        private OnDismissListener outsideDismissListener;
    
        private final OnDismissListener dismissListener = new OnDismissListener() {
    
            @Override
            public void onDismiss(DialogInterface dialog) {
                final ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
                        PropertyValuesHolder.ofInt("alpha", (int) (255 * dimAmount), 0));
                animator.setTarget(dimDrawable);
                animator.setDuration(animDuration);
                animator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        ViewGroupOverlay overlay = root.getOverlay();
                        overlay.remove(dimDrawable);
                    }
                });
                animator.start();
                if (outsideDismissListener != null)
                    outsideDismissListener.onDismiss(dialog);
            }
        };
    
    
        @TargetApi(Build.VERSION_CODES.N)
        public DatePickerDialogCustomDim(@NonNull Context context) {
            this(context, 0);
        }
    
        @TargetApi(Build.VERSION_CODES.N)
        public DatePickerDialogCustomDim(@NonNull Context context, @StyleRes int themeResId) {
            this(context, themeResId, null, -1, -1, -1);
            init(context);
        }
    
        public DatePickerDialogCustomDim(@NonNull Context context,
                                         @Nullable OnDateSetListener listener,
                                         int year,
                                         int month,
                                         int dayOfMonth) {
            this(context, 0, listener, year, month, dayOfMonth);
        }
    
        public DatePickerDialogCustomDim(@NonNull Context context,
                                         @StyleRes int themeResId,
                                         @Nullable OnDateSetListener listener,
                                         int year,
                                         int monthOfYear,
                                         int dayOfMonth) {
            super(context, themeResId, listener, year, monthOfYear, dayOfMonth);
            init(context);
        }
    
        private void init(Context context) {
            root = ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
            super.setOnDismissListener(dismissListener);
        }
    
        public void setDimAmount(@FloatRange(from = 0, to = 1f) float dim) {
            dimAmount = dim;
        }
    
        @Override
        public void show() {
            super.show();
            dimDrawable = new ColorDrawable(Color.WHITE); // a dim color
            dimDrawable.setBounds(0, 0, root.getWidth(), root.getHeight());
    
            ViewGroupOverlay overlay = root.getOverlay();
            overlay.add(dimDrawable);
    
            ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(dimDrawable,
                    PropertyValuesHolder.ofInt("alpha", 0, (int) (255 * dimAmount)));
            animator.setTarget(dimDrawable);
            animator.setDuration(animDuration);
            animator.start();
        }
    
        @Override
        public void setOnDismissListener(@Nullable OnDismissListener listener) {
            outsideDismissListener = listener;
        }
    }
    

    它与样式结合使用(主要禁用暗淡)

    <style name="DatePickerDialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorAccent">@color/accent</item>
        <item name="android:textColorLink">@color/primary</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-08
      • 2019-09-27
      • 1970-01-01
      • 2011-03-12
      • 2012-04-21
      相关资源
      最近更新 更多