一、Dialog的全屏设置
public class FullScreenDialog extends Dialog {
public FullScreenDialog(@NonNull Context context) {
super(context);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置一个布局
setContentView(R.layout.dialog_test_fullscreen);
//设置window背景,默认的背景会有inset值,不能全屏。当然不一定要是透明,你可以设置其他背景,替换默认的背景即可。
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//一定要在setContentView之后调用,否则无效
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
}
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="16dp"
android:insetTop="16dp"
android:insetRight="16dp"
android:insetBottom="16dp">
<shape android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="@android:color/white" />
</shape>
</inset>
其中有两点需要说一下
1. setContentView可以在外部调用,不一定要在自定义的Dialog里面。同理,getWindow().setBackgroundDrawable和getWindow().setLayout也可以在外部调用。如下,
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_test_fullscreen);
dialog.show();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
2. 背景色可以通过指定Dialog的Theme来完成
<style name="AppTheme.FullScreenDialog" parent="ThemeOverlay.AppCompat.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
Dialog dialog = new Dialog(this, R.style.AppTheme_FullScreenDialog);
dialog.setContentView(R.layout.dialog_test_fullscreen);
dialog.show();
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
二、AppCompatDialog全屏设置
首先,AppCompatDialog是继承于Dialog的,但是加了一些support v7的一些特性和定制。如果我们直接使用上面的方式来设置,会发现达不到全屏效果,对话框的最顶部会出现部分空白。如下:
这个时候,需要额外设置一个属性dialog.supportRequestWindowFeature(Window.FEATURE_NO_TITLE),话说这个属性我找了很久,最后在support design的BottomSheetDialog看到的。如下:
AppCompatDialog dialog = new AppCompatDialog(this, R.style.AppTheme_FullScreenDialog);
dialog.supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_test_fullscreen);
dialog.show();
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
如果是自定义继承于AppCompatDialog,可以直接在构造方法里设置这个属性,完整示例如下:
public class FullScreenDialog extends AppCompatDialog {
public FullScreenDialog(@NonNull Context context) {
super(context);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置一个布局
setContentView(R.layout.dialog_test_fullscreen);
//设置window背景,默认的背景会有Padding值,不能全屏。当然不一定要是透明,你可以设置其他背景,替换默认的背景即可。
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//一定要在setContentView之后调用,否则无效
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
}
三、Supportv7 AlertDialog全屏设置
AlertDialog继承于AppCompatDialog,但是不用设置supportRequestWindowFeature,设置全屏的方式,和普通Dialog一样。如下:
AlertDialog dialog =
new AlertDialog.Builder(this).setTitle("Title")
.setMessage("Message")
.setNegativeButton("Cancel", null)
.setPositiveButton("Sure", null)
.create();
dialog.show();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
四、修改状态栏颜色
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0 全透明实现
Window window = getWindow();
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
getWindow().setAttributes(lp);
} else {//4.4 全透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES是允许在刘海区域布局
还有其他两种