46

有没有办法让我的对话框视图全屏,即对话框占据整个屏幕(如活动)。我尝试使用 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>
4

12 回答 12

104

给它的构造函数一个非对话框主题,例如android.R.style.Themeandroid.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();
于 2012-04-16T11:53:28.477 回答
35

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

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
dialog.setContentView(R.layout.mydialog2);
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
于 2012-04-16T12:05:56.700 回答
20

尝试这个

dialog = new Dialog(context,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
于 2013-04-27T06:47:43.360 回答
4

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

于 2016-03-29T04:10:56.700 回答
3

尝试将您的包装dialog_custom_layout.xmlRelativeLayout. 这对我有用。

于 2016-09-23T07:43:57.560 回答
3

答案对我不起作用(sdk 21,三星银河 s5)。经过搜索和测试,我发现设置全屏对话框的关键点是<item name="android:windowIsFloating">false</item>在你的对话框样式中。sdk 中大多数样式的 Dialog 都设置为 true!将其设置为 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。希望能帮助到某人。

于 2016-08-05T02:19:22.477 回答
3

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

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

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

或者

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

这将全屏显示对话框。

于 2017-06-26T11:12:39.867 回答
1

试试这个代码

protected void openDialog() {
        Dialog dialog = new Dialog(this);
        dialog.addContentView(new View(this), (new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)));
        dialog.show();
    }
于 2012-04-16T11:58:11.483 回答
1

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

Dialog dialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
于 2018-01-23T13:05:27.763 回答
1

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

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);
}
于 2017-05-25T15:37:19.777 回答
1

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

第 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();
    }
}
于 2019-02-24T04:18:18.587 回答
0

带有数据绑定的全屏对话框

    fun showUserQrCode(
    mContext: Activity,
    showAddMore: Boolean,
    message: String?,
    skipClick: View.OnClickListener?,
    addMoreClick: View.OnClickListener?
) {

    val dialog = Dialog(mContext, android.R.style.Theme_Black_NoTitleBar_Fullscreen)
    val discountBinding: PopupUserQrCodeBinding = DataBindingUtil.inflate(
        LayoutInflater.from(mContext),
        R.layout.popup_user_qr_code,
        null,
        false
    )
    discountBinding.btnSkip.setOnClickListener(skipClick)


    dialog.setContentView(discountBinding.root)
    dialog.show()
}

无绑定

    val dialog = Dialog(mContext, android.R.style.Theme_Black_NoTitleBar_Fullscreen)
    dialog.setContentView(R.layout.slider_layout)
    dialog.show()
于 2021-09-13T13:22:14.747 回答