一、Dialog 的Window 的DecorView 的边距
常见问题:Dialog 无法设置全屏
原因:Window 的 LayoutParams 为 WindowManager.LayoutParams.MATCH_PARENT 时,并且没有为 Window 对应的 DecorView 设置 背景色时:
- 没有设置android:windowBackground
- 没有手动为DecorView 设置背景色。
此时DecorView 会 为自己设置padding 。导致Dialog 内容无法充满全屏。
解决方案:设置背景。
<style name="ShareDialog" parent="android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/white</item>
<item name="android:windowBackground">@android:color/white</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
二、style 属性 android:windowBackground 与 DecorView 的背景
Window 的背景最终是设置到到对应的DecorView 背景。
三、DialogFragment 的 Window 的LayoutParams变化
- 默认的 Dialog 的 Window 的LayoutParams 在window.setContentView(dialogView)调用前,为 MATCH_PARENT。
- Dialog 调用window.setContentView(dialogView) 方法设置内容后,Window 的LayoutParams 被修改为WRAP_CONTENT。 所以如果想要修改Window 的 LayoutParams ,需要在setContentView方法调动后。
- DialogFragment 的onStart 调用时,会重新设置 Window 的LayoutParams 为WRAP_CONTENT。
所以,如果想要使用Window 为MATCH_PARENT 模式,需要在onStart 调用时,重新设置Window 的LayoutParams 为MATCH_PARENT。
四、Dialog 的ContentView 的LayoutParams与Window的关系
常见问题:Dialog 对应的布局文件match_parent 失效
根据View 的 onMeasure( MesureSpec spec) ,MesureSpec 的生成原则 。取决于 父View的MesaureSpec 和自身的LayoutParams 。
- DecorView 的 LayoutParams 与Window 是保持一致的 。
- 当父View 为 WRAP_CONTENT。自身布局文件中的 width 不管为 wrap_content还是 match_parent,都没啥用,MesureSpec 都是 AT_MOST 模式。
所以想要控制Dialog 对应布局文件的 大小 ,需要设置Window 的LayoutParams。