[英]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信息屏幕快照: 日志问题:
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 getWindow.setLayout(int width, int height)
; getWindow.setLayout(int width, int height)
; What is the unit of the width and height? 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_PARENT
, WRAP_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
. -1
是WindowManager.LayoutParams.width
和WindowManager.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 afterdialog.show()
.在
dialog.show()
之后执行dialogWindow.setLayout(500,500)
dialog.show()
。 When I put it in front ofdialog.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_CONTENT
和MATCH_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));
}
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:minHeight
和android: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.