Answers
这应该就是你想实现的
位于底部、与屏幕等宽
的
DiaglogFragment
了。
(你问题描述里的“适应屏幕”,我理解的是“与屏幕等宽”)
自定义一个位于屏幕底部、与屏幕等宽的DialogFragment
public class DatePickerDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// 使用不带theme的构造器,获得的dialog边框距离屏幕仍有几毫米的缝隙。
// Dialog dialog = new Dialog(getActivity());
Dialog dialog = new Dialog(getActivity(), R.style.CustomDatePickerDialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // must be called before set content
dialog.setContentView(R.layout.dialog_datepicker);
dialog.setCanceledOnTouchOutside(true);
// 设置宽度为屏宽、靠近屏幕底部。
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
window.setAttributes(wlp);
return dialog;
}
<!-- res/values/styles.xml -->
<style name="CustomDatePickerDialog" parent="@style/AppTheme">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:windowIsFloating">true</item>
</style>
一个Demo
rpgonly
answered 9 years, 2 months ago