简体   繁体   中英

android - custom dialog changes the view background color

This is very weird , I've a timePicker in my custom dialog , the problem is it's looks nice on some version of android but in android 5.0.2 , it gets a black background color like this :

在此处输入图片说明

as you can see, the clock background is changed but I've made no changes .

final Dialog dialog = new Dialog(SabadKharid_s1.this, R.style.DialogStyler2);
            dialog.setContentView(R.layout.dialogtime);

            timePicker = (TimePicker) dialog.findViewById(R.id.timePicker);
            timePicker.setIs24HourView(true);
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
            lp.copyFrom(dialog.getWindow().getAttributes());
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            dialog.show();
            dialog.getWindow().setAttributes(lp);

this is the style I use :

                <style name="DialogStyler2" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowNoTitle">true</item>
</style>

How can I fix this problem ?

what ever you pop up in your screen ... the dialogues may have a default theme. Try using a custom theme for your pop ups ... it will change background color or set custom view for pop up and in layout set parent background as android:background="@andrid:color/transparent"

I can not think of a reason why you have used the time picker in a dialog where you can just use the Material Time Pickers directly.

You can use this for a much easier solution. https://github.com/wdullaer/MaterialDateTimePicker

 TimePickerDialog timePickerDialog = TimePickerDialog.newInstance(
            new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute, int second) {
                  //Do your thing
    );
    timePickerDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialogInterface) {
            //do your thing for cancel event
        }
    });
    timePickerDialog.setAccentColor(ContextCompat.getColor(mContext, R.color.colorPrimary));

    timePickerDialog.show(getFragmentManager(), TAG);

Try using TimePickerDialog .

    // Initialize time picker
    Calendar c = Calendar.getInstance();
    int curHour = c.get(Calendar.HOUR_OF_DAY);
    int curMinute = c.get(Calendar.MINUTE);
    boolean is24HourView = true;

    // Construct time picker dialog
    TimePickerDialog dialogTimePicker = new TimePickerDialog(this,
            new TimePickerDialog.OnTimeSetListener() {

                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) 
                {

                    // Do something 
                }
            }, curHour, curMinute, is24HourView);


    // Show time picker dialog
    dialogTimePicker.setTitle("Time Picker");
    dialogTimePicker.show();

Hope this will help~

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