android - Custom view's measurements -
i creating custom view , in layout file, set width 200dp, , height 200dp.
<com.example.baoshuai.mydefineview.mytextview android:layout_width="300dp" android:layout_height="300dp" android:layout_margin="30dp" android:text="hello,sunshine" android:textallcaps="false" android:paddingtop="40dp" android:textalignment="center"/> i using getwidth() or getmeasuredwidth() view's width, value not 200dp, 700dp. here measurements:
@override protected void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawcolor(color.blue); int width = getwidth(); int height = getheight(); log.d("get","width:"+width+" height:"+height); int measuredwidth = getmeasuredwidth(); int measuredheight = getmeasuredheight(); log.d("getmeasured","measuredwidth:"+measuredwidth+" measuredheight:"+measuredheight); } here screenshot:
android:layout_width="300dp" android:layout_height="300dp" value defining in dp. when programatically width , height int width = getwidth(); value in pixels.
so it's seems different values same.
you can check same applying values in px in layout , displaying programatically.
refere https://stackoverflow.com/a/2025541/3817374 more information different units in android.

Comments
Post a Comment