Java 对话框getWidth和getHeight不返回任何整数
我已经实现了StackOverflow上的所有功能。这是我的密码: 主要活动:Java 对话框getWidth和getHeight不返回任何整数,java,android,dialog,paint,Java,Android,Dialog,Paint,我已经实现了StackOverflow上的所有功能。这是我的密码: 主要活动: statusDialog.getWindow().setAttributes(lp); statusDialog.show(); final LinearLayout drawbar = (LinearLayout) statusDialog.findViewById(R.id.drawbar); int barWidth = drawbar.getWi
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));
}
trabr.xml:
<LinearLayout
android:id="@+id/drawbar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
statusDialog.show函数工作正常,并显示它应该显示的对话框。我试图在屏幕上画一个条,在布局被绘制到屏幕上之后,在我这里指定的布局中水平填充内容。如果我将布局高度硬编码为[N]dp,我会得到一个像素计数,但否则所有内容都会返回-1,并且我无法用canvas.drawRect(0,0,barWidth,barHeight,paint)动态填充最后一个注释掉的行代码>
我不知道我做错了什么。哈尔普。我不完全确定这里的真实答案,但我发现我的问题完全无关,是由我自己的无知/预期行为造成的
480x800(或任意大小)位图将自动缩放到我添加到的填充父视图中。基本上,我在一个无关紧要的问题上浪费了2天,因为我试图计算进度条包装的宽度。tl;dr,在填充或匹配父视图的测量视图上,仅获得-1的返回整数似乎是正常的
themoreyouknow.gif