DialogFragment全屏加控件显示在底部
大家都知道dialogfragment单纯的全屏显示很容易(什么什么Match_parent解决),单纯的显示在底部也很容易(什么什么Gravity.Bottom解决),但是又要全屏又要显示在底部就不容易了。
为什么呢?全屏显示,把几个控件显示在底部,你会发现这个控件神奇的消失了。
就像这样,上半部分是0dp,weight=1,到了手机上,下面这些控件不会显示,除非你上半部分的0dp改成固定的长度。
就算你使用这样的代码
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/pop_content" android:background="@color/colorPrimary"> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/pop_close" android:layout_gravity="center_horizontal|bottom"/> </FrameLayout>
他会全屏变成colorprimary的颜色,并不会显示imageview。
1.使用这样的方法
WindowManager.LayoutParams attributes = window.getAttributes(); attributes.width=WindowManager.LayoutParams.MATCH_PARENT; attributes.height=WindowManager.LayoutParams.MATCH_PARENT-getBarHeight();他会全屏显示,但是imageview不显示
2.使用这样的方法
这种方法,他会显示imageview,但是通知栏被遮住了,我想通知栏不被遮住.因为我原本界面是显示通知栏的,弹出dialog后整个界面往上滑动.这样的效果不好.
this.setStyle(DialogFragment.STYLE_NORMAL,android.R.style.Theme_NoTitleBar_Fullscreen);
WindowManager.LayoutParams attributes = window.getAttributes(); attributes.width=WindowManager.LayoutParams.MATCH_PARENT; // attributes.height=WindowManager.LayoutParams.MATCH_PARENT-getBarHeight();
总结下来就是:1要全屏显示,2通知栏不被顶掉;
那我就自定义一个样式
<style name="ShowTitleDontShowActionBar"> <item name="android:windowNoTitle">false</item> <item name="android:layout_height">match_parent</item> <item name="android:fitsSystemWindows">true</item> <item name="android:windowActionBar">false</item> </style>
this.setStyle(DialogFragment.STYLE_NORMAL,R.style.ShowTitleDontShowActionBar);
Dialog dialog = getDialog(); if(dialog!=null){ Window window = dialog.getWindow(); if(window!=null){ WindowManager.LayoutParams attributes = window.getAttributes(); attributes.width=WindowManager.LayoutParams.MATCH_PARENT; // attributes.height=WindowManager.LayoutParams.MATCH_PARENT-getBarHeight(); // attributes.gravity= Gravity.BOTTOM; window.setAttributes(attributes); // window.setBackgroundDrawable(null); window.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(getContext(),R.color.half_translucent))); } }
要显示title(就是通知栏),要全屏height=match_parent.好了下面两行代码不知道有没有用,加上再说。完全O鸡巴K,问题解决