Android 全屏自定义对话框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10173544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:44:48  来源:igfitidea点击:

Custom Dialog in full screen?

androiddialogfullscreen

提问by Bob

Is there any way to make my Dialog view full screen, i.e dialog occupy the entire screen (like an Activity). I tried using the LayoutParams and styles like <item name="android:windowFullscreen">true</item>but nothing seems to be working.

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

I found a way of getting rid of the Title bar, but couldn't find a way to put a dialog in full screen. So can any one suggest me a way to do it.

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

<?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>

回答by Renard

Give its constructor a non-dialog theme, such as android.R.style.Themeor android.R.style.Theme_Light.

为其构造函数提供一个非对话框主题,例如android.R.style.Themeandroid.R.style.Theme_Light

Code by @Bob.

@Bob 的代码。

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

回答by Nibha Jain

Following code works in my case :

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

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

回答by Lakshmanan

Try this

尝试这个

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

回答by Allen

1.custom your dialog style

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 :custom your dialog

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();

回答by AssIstne

The answer does not work for me(sdk 21, samsung galaxy s5). After searching and testing, I find that the key point for setting a dialog for fullscreen is the <item name="android:windowIsFloating">false</item>in your dialog style. Most styles of Dialog in sdk set it true! Set it false in a style and use it in

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

AlertDialog.Builder = new AlertDialog.Builder(getActivity(), R.style.YourStyle);

your style may look like

你的风格可能看起来像

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

after setting it false, it should be fullscreen. And more, you can use

设置为false后,它应该是全屏的。还有更多,您可以使用

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

to set it's height to wrap_content. Hope to help someone.

将其高度设置为 wrap_content。希望能帮助某人。

回答by Biswajit

For full screen dialog you should extend DialogFragmentit will provide Fragmentlife cycle methods and this is the recommended way in Android developer documents you can use simple Dialogor AlertDialogalso.

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

When you create dialog instance just use android.R.style.Theme_Black_NoTitleBar_Fullscreentheme with context like this :

当您创建对话框实例时,只需使用android.R.style.Theme_Black_NoTitleBar_Fullscreen带有上下文的主题,如下所示:

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

or

或者

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), android.R.style.Theme_Black_NoTitleBar_Fullscreen);

This will show the dialog in full screen.

这将全屏显示对话框。

回答by Ayan

Try wrapping your dialog_custom_layout.xmlinto RelativeLayout. That worked for me.

试试你的包裹dialog_custom_layout.xmlRelativeLayout。那对我有用。

回答by Hamburg is nice

I'm using a Activity with Dialog theme. In this case, fullscreen worked this way:

我正在使用带有 Dialog 主题的 Activity。在这种情况下,全屏是这样工作的:

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);
}

回答by venu46

Create your Custom Dialog as

将您的自定义对话框创建为

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

回答by Son Truong

Here are steps how to create a full screen dialog with custom layout which occupies the entire screen without any padding from each site.

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

Step 1:Define your custom dialog layout named layout_fullscreen_dialog.xml

第 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>

Step 2:Define a new style in styles.xmlnamed FullScreenDialog

第 2 步:styles.xml命名中定义新样式FullScreenDialog

<style name="FullScreenDialog" parent="Theme.AppCompat.Dialog">
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

Step 3:Write a method which creates and shows a dialog

第 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();
    }
}