【问题标题】:Dialog getWidth and getHeight return no integers no matter whatDialog getWidth 和 getHeight 无论如何都不返回整数
【发布时间】:2015-05-25 22:03:01
【问题描述】:

我已经实现了在 StackOverflow 上可以找到的所有内容。这是我的代码:

主活动:

        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:

<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); 动态填充最后注释掉的行。

我不知道我在这里做错了什么。暂停。

【问题讨论】:

    标签: java android dialog paint


    【解决方案1】:

    不完全确定这里的真正答案,但我发现我的问题完全不相关,是由我自己的无知/预期行为引起的。

    480x800(或任何大小)位图会自动缩放到我将其添加到的 FILL_PARENT 视图中。所以基本上我在一个无关紧要的问题上浪费了 2 天时间,因为我试图计算进度条包装器的宽度。 tl;博士,在填充或匹配父级的测量视图上仅返回 -1 整数似乎是正常的。

    themoreyouknow.gif

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-02
      • 2014-03-02
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多