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>

在此处输入图像描述

4

8 回答 8

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;
    }
于 2013-08-19T13:51:25.703 回答
12

首先在 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));

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

于 2013-08-19T14:10:54.317 回答
4

尝试这个

设置了 Theme.Dialog 样式的活动,请执行以下操作:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.your_layout);

    getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
}
于 2013-08-19T13:56:39.757 回答
4

这可能对某人有帮助。我想要一个对话框占据整个屏幕宽度。搜索了很多,但没有发现有用的。最后这对我有用:

mDialog.setContentView(R.layout.my_custom_dialog);
mDialog.getWindow().setBackgroundDrawable(null);

添加后,我的对话框出现在屏幕的全宽中。

于 2015-08-24T07:50:58.677 回答
3

制作如下主题的对象

Dialog objD = new Dialog(this,     android.R.style.Theme_Translucent_NoTitleBar);
于 2015-04-14T04:52:33.223 回答
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。

于 2017-09-08T06:19:50.553 回答
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);
于 2019-08-26T09:00:54.980 回答
1

我想你应该试试这个:

dialogFragment内部:

@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
        }
于 2016-09-27T14:17:13.183 回答