以下是如何创建具有自定义布局的全屏对话框的步骤,该布局占据整个屏幕,每个站点都没有任何填充。
第 1 步:定义您的自定义对话框布局,命名为layout_fullscreen_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<!-- Your dialog content here -->
</LinearLayout>
第 2 步:styles.xml
在命名中定义新样式FullScreenDialog
<style name="FullScreenDialog" parent="Theme.AppCompat.Dialog">
<item name="android:windowBackground">@android:color/transparent</item>
</style>
第 3 步:编写一个创建并显示对话框的方法
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createAndShowDialog(this);
}
// This method used to create and show a full screen dialog with custom layout.
public void createAndShowDialog(Context context) {
Dialog dialog = new Dialog(context, R.style.FullScreenDialog);
dialog.setContentView(R.layout.layout_fullscreen_dialog);
WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
dialog.getWindow().setAttributes(layoutParams);
dialog.show();
}
}