我有一个使用非常简单的自定义布局的DatePickerFragment.布局占用较小设备(HTC One m7)上的整个警报对话框,但不在Nexus 6上.有人知道为什么DatePicker不占用Nexus 6警报对话框的整个宽度或从哪里开始排除故障?谢谢!
Nexus 6(注意日期标题不会填满整个警报对话框)
HTC One m7
dialog_date.xml
<?xml version="1.0" encoding="utf-8"?> <DatePicker xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog_date_date_picker" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:calendarViewShown="false"> </DatePicker>
DatePickerFragment的onCreateDialog()
public Dialog onCreateDialog(Bundle savedInstanceState) { Date date = (Date) getArguments().getSerializable(ARG_DATE); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH); int day = calendar.get(Calendar.DAY_OF_MONTH); View v = LayoutInflater.from(getActivity()) .inflate(R.layout.dialog_date,null); mDatePicker = (DatePicker) v.findViewById(R.id.dialog_date_date_picker); mDatePicker.init(year,month,day,null); return new AlertDialog.Builder(getActivity()) .setView(v) // .setTitle(R.string.date_picker_title) .setPositiveButton(android.R.string.ok,new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog,int which) { int year = mDatePicker.getYear(); int month = mDatePicker.getMonth(); int day = mDatePicker.getDayOfMonth(); Date date = new GregorianCalendar(year,day).getTime(); sendResult(Activity.RESULT_OK,date); } }) .create(); }
解决方法
原因是AlertDialog比子DatePicker占用更多空间.
确保使用不带.Alert后缀的对话框主题(例如Theme.AppCompat.Light.Dialog).