问题
I'm currently building an app targeting API 23, with a minimum API 16. I'm facing an issue where a TimePicker is displayed within an AlertDialog. The display looks fine on Portrait mode and on Landscape mode when launching the app on Nexus 5 API <=22. However, when launching the app on a Nexus 5 API 23, the landscape mode does not correctly display the TimePicker widget, it displays only its background colors.
On Nexus 5 API 23 (Portrait): TimePicker on Portrait mode (API 23)
On Nexus 5 API 23 (Landscape): TimePicker on Landscape mode (API 23)
回答1:
You have to remove set title from time picker dialogue after that you will see time picker dialogue in Nexus 5. This issue is posted on google forum see below link :
https://code.google.com/p/android/issues/detail?id=201766
TimePickerDialog mTimePicker;
String[] time = txtTimeWriteOut.getText().toString().trim().split(":");
int hour = Integer.parseInt(time[0]);
int minute = Integer.parseInt(time[1]);
mTimePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
txtTimeWriteOut.setText(TimeUtils.getDoubleDigits(selectedHour) + ":" + TimeUtils.getDoubleDigits(selectedMinute));
passTimeOut = currentDate + TimeUtils.getDoubleDigits(selectedHour) + TimeUtils.getDoubleDigits(selectedMinute) + "00";
}
}, hour, minute, true);
// mTimePicker.setTitle("Select Time");
mTimePicker.show();
回答2:
I have this issue. This help for me:
timePicker.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
回答3:
1. If you DON'T care about loosing the title on your dialog:
If you are strictly following the official documentation on Pickers, here is what you have to do in your onCreateDialog()
method:
Instead of creating new instance of TimePickerDialog
and returning it straight away, call setTitle(null)
on it before you return it, like so:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
/* ... */
Dialog dialogFragment = new TimePickerDialog(/* ... */);
dialogFragment.setTitle(null);
return dialogFragment;
}
I would like to note, that the problem was not occurring on Android 5.0.2 (Lollipop). The dialog was without the title by default.
The problem occurred on Android 6.0 device. It displayed the very same dialog with a title.
2. If having the title on the dialog is a must for you:
You should read the source code for TimePickerDialog. No other way around.
I didn't go full rage-mode on the source code, since I had the luxury of just setting it to null
.
I believe you can find a private method setupTitle()
of the package-private class AlertController very interesting. AlertControler
is used in AlertDialog business logic.
来源:https://stackoverflow.com/questions/35494234/android-timepicker-not-displayed-well-on-landscape-mode