简体   繁体   中英

How to change the date picker background color in android

Need to change the datepicker background color from white to black. Kindly refer the attached image which shown below

How can I modify the color of the background from white to black.

My code is below.

public void setListener(DatePickerDialog.OnDateSetListener listener) {
        this.listener = listener;
    }

       @SuppressLint({"ResourceAsColor", "ResourceType"})
        @Override
        public void onStart() {
            super.onStart();
            Button positive = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE);
            positive.setTextColor(R.color.red);
            positive.setBackgroundColor(getResources().getColor(R.color.blue));
        }
    
     public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            View dialog = inflater.inflate(R.layout.dialog_datepicker, null);
            final DatePicker datePicker = (DatePicker) dialog.findViewById(R.id.date_picker);
    
            Calendar c = Calendar.getInstance();
         
    
            datePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener(){
                @Override
                public void onDateChanged(DatePicker datePicker, int i, int i1, int i2) {
                    datePicker.setMaxDate(DateUtils.addYears(c.getTime(), 0).getTime());
                }
    
            });
          return builder.create();
    }
    
     <DatePicker
            android:id="@+id/date_picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/black_with_corner"
            android:layout_gravity="center_horizontal"
            android:paddingLeft="12dp"
            android:paddingRight="4dp"
            android:calendarViewShown="false"
            android:datePickerMode="spinner"
            />

在此处输入图像描述

       <DatePicker
        android:id="@+id/date_picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/black_with_corner"
        android:backgroundTint="@color/ColorBlue"
        android:layout_gravity="center_horizontal"
        android:paddingLeft="12dp"
        android:paddingRight="4dp"
        android:calendarViewShown="false"
        android:datePickerMode="spinner"
        />

By using backgroundTint u can change the date picker background color - android:backgroundTint="@color/ColorBlue"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM