DatePickerDialog标头文本边距问题Android

时间:2018-08-17 10:59:12

标签: android android-datepicker

我在CustomDialogFragment中使用DatePickerDialog。它在所有设备上均能正常工作,但在具有OS 6.0.1(棉花糖)的Moto G手机中出现问题。 Displayng 2018和Fri位于最左端。但是在其他设备上也可以。

以下是我的代码段和屏幕截图:-

DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(), this, year, month, day);
            datePickerDialog.getDatePicker().init(year, month, day, new DatePicker.OnDateChangedListener() {
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                    if(getArguments().getString("activity").equalsIgnoreCase("RegisterActivity")){
                        RegisterActivity.etExp.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
                    } else {
                        if (getArguments().getString("from", "").equalsIgnoreCase("Start")) {
                            SortRightFragment.tvStartDate.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
                        }
                        if (getArguments().getString("from", "").equalsIgnoreCase("End")) {
                            SortRightFragment.tvEndDate.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
                        }
                    }
                    datePickerDialog.cancel();
                }
            });

            datePickerDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.text_cancel), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (which == DialogInterface.BUTTON_NEGATIVE) {
                    }
                }
            });


            datePickerDialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.text_clear), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (which == DialogInterface.BUTTON_POSITIVE) {
                        if(getArguments().getString("activity").equalsIgnoreCase("RegisterActivity")){
                            RegisterActivity.etExp.setText("");
                        } else {
                            if (getArguments().getString("from", "").equalsIgnoreCase("Start")) {
                                SortRightFragment.tvStartDate.setText("");
                            }
                            if (getArguments().getString("from", "").equalsIgnoreCase("End")) {
                                SortRightFragment.tvEndDate.setText("");
                            }
                        }
                    }
                }
            });

enter image description here

预先感谢!

1 个答案:

答案 0 :(得分:0)

由于DatePickerDialog扩展了AlertDialog,也许这对您有用:
首先找到标题TextView,然后设置其填充:

TextView tv = datePickerDialog.findViewById(getResources().getIdentifier("alertTitle", "id", "android"));

这也可以:

TextView tv = (TextView) datePickerDialog.findViewById(R.id.alertTitle);

将填充值更改为所需的值:

tv.setPadding(2, 0, 0, 0);