java 如何获取对话框大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13515486/
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-10-31 13:03:19  来源:igfitidea点击:

How to get dialog size?

javaandroiddialogandroid-dialog

提问by slezadav

I am looking for a way to get size of a custom dialog. I went through thisquestion, but the only answer given is pretty useless, because if I try mDialog.getWindow().getAttributes().height;it only returns -2, which is a constant for WRAP_CONTENTattribute which I set to dialog. How can I get the size of it. I want to know the siye for the background image.

我正在寻找一种获取自定义对话框大小的方法。我经历了这个问题,但给出的唯一答案是非常无用的,因为如果我尝试mDialog.getWindow().getAttributes().height;它只返回 -2,这是WRAP_CONTENT我设置为对话框的属性的常量。我怎样才能得到它的大小。我想知道背景图像的四叶。

回答by Korniltsev Anatoly

Give it a try:

试一试:

mDialog.getWindow().getDecorView().getHeight() 

回答by Michal

Actually, in Android it doesn't work like in iOS - you can't get the size of the Viewitself, what you can do, though, is to ask for the size of the ROOTlayout of that view.

实际上,在 Android 中它不像在 iOS 中那样工作 - 您无法获得View自身的大小,但是您可以做的是询问该视图的ROOT布局的大小。

e.g.:

例如:

myDialog.this.findViewById(R.id.dialog_root_layout).getHeight());

myDialog.this.findViewById(R.id.dialog_root_layout).getHeight());

回答by CodeToLife

@Kormilsev Anatoliy has answered correct and I am just improving. So in the class you inherit from Dialog class just override the method:

@Kormilsev Anatoliy 回答正确,我只是在改进。因此,在您从 Dialog 类继承的类中,只需覆盖该方法:

@Override
public void onWindowFocusChanged (boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    height = getWindow().getDecorView().getHeight();
}

回答by Alexey Ishkov

In case, if you have your own XML layouts for custom dialog.

以防万一,如果您有自己的自定义对话框的 XML 布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/dialog_main_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@color/primaryBackground">

    /* whatever you want here */

</android.support.constraint.ConstraintLayout>

In activity:

在活动中:

    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.popup_gameover);

    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface d) {
            View view = dialog.findViewById(R.id.dialog_main_layout);
            int width = view.getWidth();
            int height = view.getHeight();

            ...
        }
    });

This widthand heightexacly as expected.

widthheight预期的一样。