Android/Android 일반

dimen에서 숫자만 가져오기

lipnus 2019. 5. 28. 16:44
반응형

In my dimens.xml I have

<dimen name="test">48dp</dimen>

In code If I do

int valueInPixels = (int) getResources().getDimension(R.dimen.test)

this will return 72 which as docs state is multiplied by density of current phone (48dp x 1.5 in my case)

exactly as docs state :

Retrieve a dimensional for a particular resource ID. Unit conversions are based on the current DisplayMetrics associated with the resources.

so if you want exact dp value just as in xml just divide it with DisplayMetrics density

int dp = (int) (getResources().getDimension(R.dimen.test) / getResources().getDisplayMetrics().density)

dp will be 48 now



참고: https://stackoverflow.com/questions/11121028/load-dimension-value-from-res-values-dimension-xml-from-source-code

반응형

'Android > Android 일반' 카테고리의 다른 글

파란색 구글로그인 버튼 (Google Login Dark Version)  (0) 2019.06.13
Drawable xml 버튼  (0) 2019.06.13
코딩으로 뷰의 크기확인 및 수정  (0) 2019.05.20
ViewPager양쪽에 여백  (0) 2019.05.15
DisplayMetrics.density  (0) 2019.05.15