Android之DialogFragment宽度充满屏幕

项目中,用DialogFragment提示对话框时,公司UI非得把对话框宽度充满屏幕并在屏幕底部,折腾了半天才搞定。在DialogFragment都是默认有一定的padding,但是在API里并没有设置方法。查了半天资料都是AlerDialog设置宽度充满屏幕,DialogFragment的寥寥无几,解决之余记录下来。如果碰到同样的问题,可以参考。

1.重写onCreate方法设置Fragment的styled为DialogFragment.STYLE_NO_FRAME。据官方文档介绍,此样式不画任何框架,;通过onCreateView(LayoutInflater,ViewGroup)返回完全负责绘制对话框的视图层。DialogFragment的Theme,则根据需求自己定义

<span style="font-size:14px;">setStyle(DialogFragment.STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);</span>
2.重写onCreateView,创建Dialog。根据我多次试验,不要重写onCreateDialog来创建Dialog.如果使用此方法,Dialog的显示并不能够按照我们的意图显示。使用AlertDialog创建的Dialog,是浮动在Fragment的上方,就算后续设置Dialog的宽度高度,仍不能充满屏幕。

(a).布局文件:

<span style="font-size:14px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_selDate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:textSize="18sp" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:background="@color/colorAccent" />

    <DatePicker
        android:id="@+id/dp_dateSel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:calendarViewShown="false"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:background="@color/colorAccent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_dateSelCancel"
            style="@style/SplitHorizontal"
            android:text="@string/do_cancel"
            android:textSize="18sp" />

        <Button
            android:id="@+id/btn_dateSelOk"
            style="@style/SplitHorizontal"
            android:text="@string/do_ok"
            android:textSize="18sp" />
    </LinearLayout>

</LinearLayout></span>
(b).重写onCreateView方法

<span style="font-size:14px;">	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.dia_date_sel, container, false);
		
		return view;
	}</span>
(c).重写onStart方法,设置Dialogde
<span style="font-size:14px;">	public void onStart() {
		super.onStart();
		Window win = getDialog().getWindow();
		// 一定要设置Background,如果不设置,window属性设置无效
		win.setBackgroundDrawable( new ColorDrawable(getResources().getColor(R.color.back_dia)));
		
		DisplayMetrics dm = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay().getMetrics( dm );
		
		LayoutParams params = win.getAttributes();
		params.gravity = Gravity.BOTTOM;
		// 使用ViewGroup.LayoutParams,以便Dialog 宽度充满整个屏幕
		params.width =  ViewGroup.LayoutParams.MATCH_PARENT;
		params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
		win.setAttributes(params);
		
	}</span>
(d)运行测试效果:弹出的Dialog是一个时间选择器



使用AlerDialog创建Dialog,使Dialog充满屏幕的方法为在dialog.show()后面加上以下代码:

<span style="font-size:14px;">WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.width = (int)(display.getWidth()); //设置宽度
dialog.getWindow().setAttributes(lp);</span>



集思广益、坚持不懈的总结,总会进步!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值