自定义对话框全屏?

时间:2012-04-16 11:51:19

标签: android dialog fullscreen

有没有办法让我的Dialog视图全屏显示,即对话框占据整个屏幕(如活动)。我尝试使用LayoutParams和样式 <item name="android:windowFullscreen">true</item>但似乎没有任何效果。

我找到了摆脱标题栏的方法,但找不到将对话框全屏显示的方法。所以任何人都可以建议我这样做。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="@android:style/Theme.Dialog">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@color/dialog_background</item>
</style>
</resources>

11 个答案:

答案 0 :(得分:89)

为其构造函数指定一个非对话框主题,例如 android.R.style.Theme android.R.style.Theme_Light

@Bob编码。

Dialog dialog = new Dialog(context, android.R.style.Theme_Light); 
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.MyCustomDialogLayout); 
dialog.show();

答案 1 :(得分:31)

以下代码适用于我的情况:

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.mydialog2);
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

答案 2 :(得分:17)

试试这个

dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

答案 3 :(得分:2)

1.自定义对话框样式

 <style name = "MyDialog" >
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name = "android:windowContentOverlay" >@null</item >
        <item name = "android:colorBackgroundCacheHint" >@null</item >
        <item name = "android:backgroundDimEnabled">true</item>
        <item name = "android:windowBackground" >@android:color/transparent</item >
    </style >

2:自定义对话框

WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
            int width = wm.getDefaultDisplay().getWidth();
            int height = wm.getDefaultDisplay().getHeight();
            final Dialog dialog = new Dialog(this, R.style.MyDialog);
            View view = LayoutInflater.from(this).inflate(R.layout.layout_dialog, null);

            WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;

            dialog.setContentView(view);
            dialog.getWindow().setGravity(Gravity.BOTTOM);
            dialog.getWindow().setWindowAnimations(R.style.mydialog_Style);
            dialog.show();

答案 4 :(得分:1)

答案对我不起作用(sdk 21,samsung galaxy s5)。在搜索和测试之后,我发现设置全屏对话框的关键点是对话框样式中的<item name="android:windowIsFloating">false</item>。 sdk中的大多数Dialog风格都是真的!在样式中将其设置为false并在

中使用它
AlertDialog.Builder = new AlertDialog.Builder(getActivity(), R.style.YourStyle);

你的风格可能看起来像

<style name="YourStyle">
    <item name="android:windowIsFloating">false</item>
    ...
</style>

设置为false后,应为全屏。而且,您可以使用

dialog.getWindow.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);

将它的高度设置为wrap_content。 希望能帮助别人。

答案 5 :(得分:1)

尝试将dialog_custom_layout.xml打包到RelativeLayout。这对我有用。

答案 6 :(得分:1)

对于全屏对话框,您应该扩展DialogFragment它将提供Fragment生命周期方法,这是Android开发人员文档中推荐的方法,您可以使用简单的Dialog或{{1}也是。

当你创建对话框实例时,只需使用AlertDialog主题和上下文:

android.R.style.Theme_Black_NoTitleBar_Fullscreen

Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_Black_NoTitleBar_Fullscreen);

这将全屏显示对话框。

答案 7 :(得分:0)

试试这段代码

protected void openDialog() {
        Dialog dialog = new Dialog(this);
        dialog.addContentView(new View(this), (new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)));
        dialog.show();
    }

答案 8 :(得分:0)

我正在使用带对话主题的活动。在这种情况下,全屏以这种方式工作:

int mUIFlag = View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(mUIFlag);

        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        setContentView(R.layout.lockscreen_activity);
}

答案 9 :(得分:0)

将自定义对话框设置为

Dialog dialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

答案 10 :(得分:0)

以下是创建具有自定义布局的全屏对话框的步骤,该对话框占据了整个屏幕,而每个站点都没有填充。

步骤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();
    }
}
相关问题