簡體   English   中英

對話框getWidth和getHeight不管什么都不返回整數

[英]Dialog getWidth and getHeight return no integers no matter what

我已經實現了我可以在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);動態填充最后注釋的行canvas.drawRect(0, 0, barWidth, barHeight, paint);

我不知道我在做什么錯。 幫助。

不能完全確定此處的真實答案,但我發現我的問題完全無關緊要,並且是由我自己的無知/預期行為引起的。

480x800(或任何大小)的位圖會自動縮放到我添加到的FILL_PARENT視圖中。 因此,基本上,我在無用的問題上浪費了2天,因為我試圖計算進度條包裝程序的寬度。 tl; dr,僅在填充或匹配父級的測量視圖上獲得返回整數-1似乎是正常的。

themoreyouknow.gif

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM