[英]Dialog getWidth and getHeight return no integers no matter what
I have implemented everything I can find here on StackOverflow. 我已经实现了我可以在StackOverflow上找到的所有内容。 Here is my code:
这是我的代码:
MainActivity: 主要活动:
statusDialog.getWindow().setAttributes(lp);
statusDialog.show();
final LinearLayout drawbar = (LinearLayout) statusDialog.findViewById(R.id.drawbar);
int barWidth = drawbar.getWidth();
int barHeight = drawbar.getHeight();
DrawBar(drawbar);
}
public void DrawBar(View v){
LinearLayout statusDialog = (LinearLayout) v;
Paint paint = new Paint();
paint.setColor(Color.parseColor("#CD5C5C"));
Bitmap bg = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bg);
View bar = statusDialog.findViewById(R.id.drawbar);
LinearLayout test = (LinearLayout) statusDialog.findViewById(R.id.drawbar);
int barWidth2 = statusDialog.getLayoutParams().width;
int barHeight2 = statusDialog.getLayoutParams().height;
int barWidth3 = v.getLayoutParams().width;
int barHeight3 = v.getLayoutParams().height;
int barWidth4 = bar.getLayoutParams().width;
int barHeight4 = bar.getLayoutParams().height;
int barWidth5 = v.getMeasuredWidth();
int barHeight5 = v.getMeasuredHeight();
int barWidth6 = statusDialog.getWidth();
int barHeight6 = statusDialog.getHeight();
test.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED))
int barheight7 = test.getMeasuredHeight();
int barwidth7 = test.getMeasuredWidth();
canvas.drawRect(0, 0, 50, 50, paint);
//canvas.drawRect(0, 0, barWidth, barHeight, paint);
statusDialog.setBackgroundDrawable(new BitmapDrawable(this.getResources(), bg));
}
drawbar.xml: drawbar.xml:
<LinearLayout
android:id="@+id/drawbar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
The statusDialog.show function works fine and shows the dialog that it should. statusDialog.show函数可以正常工作,并显示应显示的对话框。 I am trying to paint a bar to the screen that fills content horizontally in the layout I'm specifying here, after the layout is drawn to the screen.
我试图在屏幕上绘制条形,以便在将布局绘制到屏幕上之后在此处指定的布局中水平填充内容。 If I hard-code the layout height to [N]dp, I get a pixel count back, but otherwise everything just returns -1 and I'm not able to dynamically fill the last commented out line with
canvas.drawRect(0, 0, barWidth, barHeight, paint);
如果我将布局高度硬编码为[N] dp,则会得到一个像素计数,但是否则,其他所有内容只会返回-1,并且无法使用
canvas.drawRect(0, 0, barWidth, barHeight, paint);
动态填充最后注释的行canvas.drawRect(0, 0, barWidth, barHeight, paint);
. 。
I don't know what I'm doing wrong here. 我不知道我在做什么错。 HALP.
帮助。
Not entirely sure of the real answer here, but I figured out that my problem was completely irrelevant and caused by my own ignorance/expected behavior. 不能完全确定此处的真实答案,但我发现我的问题完全无关紧要,并且是由我自己的无知/预期行为引起的。
The 480x800 (or whatever size) bitmap is automatically scaled into the FILL_PARENT View I was adding it to. 480x800(或任何大小)的位图会自动缩放到我添加到的FILL_PARENT视图中。 So essentially I wasted 2 days on a problem that didn't matter, as I was trying to calculate the width of a progress bar wrapper.
因此,基本上,我在无用的问题上浪费了2天,因为我试图计算进度条包装程序的宽度。 tl;dr, only getting return integers of -1 on measured views that fill or match parent seems to be normal.
tl; dr,仅在填充或匹配父级的测量视图上获得返回整数-1似乎是正常的。
themoreyouknow.gif themoreyouknow.gif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.