Android Gets the value of the variable inside the Dimens.xml file via Getdimension,getdimensionpixeloffset and Getdimensionpixelsize

Source: Internet
Author: User

Three variables are written in the Dimens.xml:

<dimen name="activity_vertical_margin1" >16dp</dimen>

<dimen name="activity_vertical_margin2" >16px</dimen>

<dimen name="activity_vertical_margin3" >16sp</dimen>

Read these three variables:

float a1=getresources (). Getdimension (R.dimen.activity_vertical_margin1);

int a2=getresources (). Getdimensionpixeloffset (R.dimen.activity_vertical_margin1);

int a3=getresources (). Getdimensionpixelsize (R.dimen.activity_vertical_margin1);

float b1=getresources (). Getdimension (r.dimen.activity_vertical_margin2);

int b2=getresources (). Getdimensionpixeloffset (r.dimen.activity_vertical_margin2);   int b3=getresources (). Getdimensionpixelsize (r.dimen.activity_vertical_margin3);

float c1=getresources (). Getdimension (r.dimen.activity_vertical_margin3);

int c2=getresources (). Getdimensionpixeloffset (r.dimen.activity_vertical_margin3);

int c3=getresources (). Getdimensionpixelsize (r.dimen.activity_vertical_margin3);

LOG.I ("Test", "getdimension= " +a1+ ", getdimensionpixeloffset=" +a2+ ",getdimensionpixelsize=" +  A3);

LOG.I ("Test", "getdimension= " +b1+ ", getdimensionpixeloffset=" +b2+ ",getdimensionpixelsize=" +  B3);

LOG.I ("Test", "getdimension= " +c1+ ", getdimensionpixeloffset=" +c2+ ",getdimensionpixelsize=" +  C3);

For device 1 (1280*720,160dpi,density=1.0):

09-: 15.202:i/test (9732): getdimension= 16.0, getdimensionpixeloffset=16, getdimensionpixelsize= 09-: 15.202:i/test (9732): getdimension= 16.0, getdimensionpixeloffset= 16,getdimensionpixelsize= 09-: 15.202:i/test (9732): getdimension= 16.0, getdimensionpixeloffset=16, getdimensionpixelsize=

For device 2 (480*800,240dpi,density=1.5):

09-: 07.376:i/test (5902): getdimension= 24.0, getdimensionpixeloffset= 24,getdimensionpixelsize= 09-: 07.376:i/test (5902): getdimension= 16.0, getdimensionpixeloffset=16, getdimensionpixelsize= 09-: 07.376:i/test (5902): getdimension= 24.0, getdimensionpixeloffset= 24,getdimensionpixelsize=

Visible getdimension and Getdimensionpixeloffset function almost, are to get a value of a dimen, if it is a DP or SP unit, multiply it by density, if it is PX, do not multiply; the difference between two functions is a return float , an int is returned.

Android Gets the value of the variable inside the Dimens.xml file via Getdimension,getdimensionpixeloffset and Getdimensionpixelsize

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.