简体   繁体   English

为什么Android AlertDialog的宽度和高度= -1

[英]Why can Android AlertDialog's width and height = -1

I have three questions to AlertDialog. 我对AlertDialog有三个问题。 First of all, I will post my code Then ask my question. 首先,我将发布我的代码,然后问我的问题。

<LinearLayout
android:id="@+id/l2"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@android:color/holo_orange_light"
xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:layout_width="200dp"
        android:layout_height="600dp"
        android:text="Test Test"
        android:textSize="40sp"
        android:background="@android:color/holo_green_light"
        android:textColor="@android:color/black"/>

This is the view to display in the AlertDialog, In the activity_main.xml, I have set a button to open the AlertDialog,The following is the code of listener. 这是要显示在AlertDialog中的视图,在activity_main.xml中,我设置了一个按钮来打开AlertDialog,以下是侦听器的代码。

  btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_window, null);
            AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(view).create();
            WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
            Log.d("TAG", "First lp width and height" + lp.width + "  " + lp.height);
            dialog.show();
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setLayout(500,500);
            WindowManager.LayoutParams lp2 = dialogWindow.getAttributes();
            Log.d("TAG", "lp2 width and height" + lp2.width + "  " + lp2.height);

        }
    });

The logcat information screenshot in the following: Log Question: 下面的logcat信息屏幕快照: 日志问题:

  1. As you can see, The width and height of Dialog equals -1, I don't know the reason. 如您所见,Dialog的宽度和高度等于-1,我不知道原因。
  2. dialogWindow.setLayout(500,500); is executed after dialog.show .When I put dialogWindow.setLayout(500,500); dialog.show之后执行。当我放置dialogWindow.setLayout(500,500); in front of dialog.show , I find it does't work. dialog.show前面,我发现它不起作用。 Why 为什么
  3. getWindow.setLayout(int width, int height) ; getWindow.setLayout(int width, int height) ; What is the unit of the width and height? 宽度和高度的单位是多少? ScreenShot 屏幕截图

1 & 2 1和2
An alertdialog is not created until you call either .show() or .create() on it. 在您调用.show().create()之前,不会创建.show() .create() So if you call the methods you're calling before creating it it will just return some default values. 因此,如果在创建之前调用要调用的方法,它将仅返回一些默认值。

If you do it like this 如果你这样做

AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(view).create();
dialog.create();
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
Log.d("TAG", "First lp width and height" + lp.width + "  " + lp.height);

it should show the right values. 它应该显示正确的值。

3 3
The same values as you would enter in xml. 与您在xml中输入的值相同。 Can be MATCH_PARENT , WRAP_CONTENT or an exact value. 可以是MATCH_PARENTWRAP_CONTENT或精确值。 The exact value is in pixels. 精确值以像素为单位。

Also see the docs for Window 另请参阅Window的文档

As you can see, The width and height of Dialog equals -1, I don't know the reason. 如您所见,Dialog的宽度和高度等于-1,我不知道原因。

-1 is the default value for both WindowManager.LayoutParams.width and WindowManager.LayoutParams.height . -1WindowManager.LayoutParams.widthWindowManager.LayoutParams.height的默认值。

-1 is also known as MATCH_PARENT which means the dialog will try to occupy as much space while respecting theme attributes such as maximum and minimum width and height. -1也称为MATCH_PARENT ,这意味着对话框将在尊重主题属性(例如最大和最小宽度和高度)的同时尝试占用尽可能多的空间。

dialogWindow.setLayout(500,500) is executed after dialog.show() . dialog.show()之后执行dialogWindow.setLayout(500,500) dialog.show() When I put it in front of dialog.show() it does't work. 当我将它放在dialog.show()前面时,它不起作用。 Why? 为什么?

Before dialog.show() or dialog.create() the AlertDialog hasn't installed its content yet. dialog.show()dialog.show()dialog.create()之前安装其内容。 It internally calls the following method of PhoneWindow : 它在内部调用以下PhoneWindow方法:

@Override
public void setContentView(View view) {
    setContentView(view, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
}

This should also explain where the default values come from. 这也应解释默认值的来源。 Your call to setLayout should now produce desired results. 您对setLayout调用现在应该会产生所需的结果。

Credit: @TimCastelijns 信用: @TimCastelijns

getWindow.setLayout(int width, int height) What is the unit of the width and height? getWindow.setLayout(int width, int height)宽度和高度的单位是什么?

The unit is pixels. 单位是像素。 You can also use WRAP_CONTENT and MATCH_PARENT constants. 您还可以使用WRAP_CONTENTMATCH_PARENT常量。

If you're going to use this method avoid hardcoding pixels since on different screens you'd get different visual results. 如果要使用此方法,请避免对像素进行硬编码,因为在不同的屏幕上您会得到不同的视觉效果。 These methods will help you: 这些方法将帮助您:

/** Precise decimal value, use for translation. */
public static float dpToPx(Context context, int dp) {
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}

/** Rounded down value, use for offsets, paddings, margins. */
public static int dpToPxOffset(Context context, int dp) {
    return (int) (dpToPx(context, dp));
}

/** Rounded up value, use for sizes. */
public static int dpToPxSize(Context context, int dp) {
    return (int) (0.5f + dpToPx(context, dp));
}

Notes: 笔记:

The following lines are ignored. 以下各行将被忽略。

android:layout_width="200dp"
android:layout_height="200dp"

layout_ prefixed attributes are set on a LayoutParams object provided by parent view. 在由父视图提供的LayoutParams对象上设置layout_前缀的属性。 Since there's no parent, there are no LayoutParams you can work with. 由于没有父级,因此没有可以使用的LayoutParams Try setting android:minHeight and android:minWidth instead. 尝试改为设置android:minHeightandroid:minWidth

Parent view for root element is determined by parameter for inflation: 根元素的父视图由膨胀参数决定:

LayoutInflater.from(context).inflate(R.layout.popup_window, /*parent*/ null);
 btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.popup_window, null);
        AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setView(view).create();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        Log.d("TAG", "First lp width and height" + lp.width + "  " + lp.height); <---------
        dialog.show();     <----------- you show the window here, so its size can be calculated. Before this, it doesn't exist. This is why you get -1 -1
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setLayout(500,500);
        WindowManager.LayoutParams lp2 = dialogWindow.getAttributes();
        Log.d("TAG", "lp2 width and height" + lp2.width + "  " + lp2.height);

    }
});

Try with dialog.show before your first Log ;) 在您的第一个日志之前尝试使用dialog.show;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:如何设置AlertDialog的宽度和高度,以及AlertDialog样式的按钮? - Android:How can I set the AlertDialog width and height,and the button of the AlertDialog style? AlertDialog AppCompat的宽度和高度 - AlertDialog AppCompat width and height 为什么Android AlertDialog没有占据屏幕的全高 - Why Android AlertDialog is not taking full height of screen 如何在Xamarin Android C#中设置AlertDialog框的宽度和高度? - How to set width and height for AlertDialog box in Xamarin android c#? 自定义inflate alertdialog布局android中的自定义高度和宽度 - custom height and width in custom inflate alertdialog layout android 在Android中,如何设置ListView项的高度和宽度? - In Android, how can I set a ListView item's height and width? 如何修复android平板电脑的宽度和高度 - How can I fix android tablet's width and height 具有自定义宽度的Android AlertDialog - Android AlertDialog with custom width Android:DialogFragment 的自定义布局没有像 AlertDialog 那样完全显示其宽度 - Android: Custom layout for DialogFragment is not showing fully in it's width like AlertDialog 如何将最大高度设置为 Android 中的 AlertDialog 内的列表视图? - How can I set the max height to a listview inside an AlertDialog in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM