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:

enter image description here

    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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -