Android 如何使对话框全屏显示

24

我需要弹出对话框充满屏幕,但在顶部和底部留有一些空间。

我已经搜索了解决方案,但可能找不到一个合适的,因为我是在onClickListener中声明它。请问有人可以提供一个解决方案吗?

活动代码:

sort.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder sort = new AlertDialog.Builder(HomeScreen.this);
                // Get the layout inflater
                LayoutInflater inflater = HomeScreen.this.getLayoutInflater();
                View sortView = inflater.inflate(R.layout.sort_layout, null);
                sort.setView(sortView);
                sort.create().show();
            }
        });

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="50dp"
    android:layout_marginTop="50dp"
    android:background="@color/white"
    android:orientation="vertical" android:layout_gravity="center">
    + some more stuff here I dont think it's relevant
</LinearLayout>

输入图像描述


请检查此链接:https://dev59.com/53M_5IYBdhLWcg3wfTA0 - Lia Pronina
已经尝试过getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT),但它没有任何作用,并且显示它已被弃用。 - Mihai Bratulescu
你必须为此制作自定义样式... - Shani Goriwal
我应该在哪里应用它? - Mihai Bratulescu
7个回答

43
在此设置您的屏幕宽度和对话框宽度。
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = width;
    lp.height = height;
    dialog.getWindow().setAttributes(lp);

或者在您的样式中创建一个类似这样的样式

 <style name="DialogTheme" parent="android:Theme.Dialog">

    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">fill_parent</item>


    <!-- No backgrounds, titles or window float -->
    <item name="android:windowBackground">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
</style>

创建一个对话框对象,就像这样

dialog = new Dialog(this, R.style.DialogTheme);

编辑::

想要获得任何设备的宽度和高度,可以按照以下方式填充。

WindowManager manager = (WindowManager) getSystemService(Activity.WINDOW_SERVICE);
    int width, height;
    LayoutParams params;

    if (Build.VERSION.SDK_INT > VERSION_CODES.FROYO) {
        width = manager.getDefaultDisplay().getWidth();
        height = manager.getDefaultDisplay().getHeight();
    } else {
        Point point = new Point();
        manager.getDefaultDisplay().getSize(point);
        width = point.x;
        height = point.y;
    }

你的方法有点可行,但是我该如何设置值以适应任何屏幕? - Mihai Bratulescu
它正在工作,但我只是使用了manager.getDefaultDisplay().getWidth(),因为point需要更高的API级别,而我正在开发2.2。 - Mihai Bratulescu
2
设置 <item name="android:windowBackground">@null</item> 不会给你一个透明的背景,但是 <item name="android:windowBackground">@android:color/transparent</item> 会。 - Ionut Negru

11

首先,在style.xml文件中创建自定义对话框样式:

<style name="full_screen_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>

然后将此主题设置为您的警报对话框:

 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this,  R.style.full_screen_dialog));

或者您可以调用新的 Activity,并在清单文件中将此自定义样式作为主题赋予该 Activity…

windowIsFloating = true 会给对话框添加起始和结束的边距,如何解决呢? - NickUnuchek

4
这可能对某些人有所帮助。 我想让一个对话框占据整个屏幕宽度。我搜索了很多,但没有找到有用的内容。最后,这个方法对我起作用了:
mDialog.setContentView(R.layout.my_custom_dialog);
mDialog.getWindow().setBackgroundDrawable(null);

在添加了这个之后,我的对话框会以全屏的宽度出现。

3
让对象像下面这个主题一样。
Dialog objD = new Dialog(this,     android.R.style.Theme_Translucent_NoTitleBar);

2
   Dialog dialog2 = new Dialog(context, R.style.DialogTheme);
    dialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog2.getWindow().setBackgroundDrawable(null);
    dialog2.setContentView(R.layout.dialog_img);
    WindowManager.LayoutParams lp = dialog2.getWindow().getAttributes();
    Window window = dialog2.getWindow();
    lp.copyFrom(window.getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    window.setAttributes(lp);
    lp.gravity = Gravity.CENTER;
    final ImageView imgprofile=(ImageView)dialog2.findViewById(R.id.img_centre);
    Picasso.with(context)
            .load(arrayImages.get(position).get("image"))
            .resize(800,1000)
            .centerInside()
            .into(imgprofile, new Callback() {

                @Override
                public void onSuccess() {

                }

                @Override
                public void onError() {
                    imgprofile.setImageResource(R.drawable.user);
                }
            });
    dialog2.show();

在你的dialog_img.xml文件中添加一个scaleType(fitXY)的ImageView。

1
我认为你应该尝试这个:
dialogFragment 中:
@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        }

1
创建全屏对话框的最简单方法是创建一个类似这样的样式:
<style name="DialogFullScreenTheme" parent="Theme.AppCompat.Light.DialogWhenLarge">
    <item name="windowNoTitle">true</item>
    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
</style>

然后:
dialog = new Dialog(this, R.style.DialogFullScreenTheme);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接