Android:Dimension
A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp.
안드로이드 개발을 하다보면 다양한 폰들의 해상도에 맞게 UI를 구성하는데 많은 어려움을 느낀다. 결과적으론 최대한 리소스들을 xml 로 정의하고 해상도에 맞게 폴더를 나누어 줘야 한다. 즉, 동적으로 리소스를 불러 사용하는 경우가 그런 경우다. 그 중 Text Size에 관련한 dimens.xml
파일에서 리소스를 가져와 setTextSize()
를 해주는데, Text가 의도한 크기보다 훨씬 크게 나오는 문제가 있다.
value-xhdpi/dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Record table row size -->
<dimen name="record_row_height">24dp</dimen>
<dimen name="record_default_text">15dip</dimen>
</resources>
위와 같이 value-xhdpi 폴더에 고해상도용 리소스를 정의할 수 있다. 물론 value-hdpi 폴더에도 같은 dimens.xml
을 정의해줘야한다. 소스상에서 해당 리소스를 사용할경우 아래와 같이 하면 된다.
이렇게 현재 세팅하는 리소스가 pixel이라고 명시해줘야한다. 문제는 getDimension()
가 pixel 형태로 리턴하기 때문이다. 우리가 dimens.xml
에 dip라고 명시해 줬기 때문에 getDimension()
은 알아서 pixel로 변환해서 리턴해준다. 그런데 setTextSize()
는 기본 단위가 pixel 이 아닌 sp라서 문제가 되는것이다.
만약 Layout XML에서 적용할 경우 아래와 같은 속성에 적용하면 된다.
dimens.xml
에 설정된 수치값을 DIP로 가져오는 방법
안드로이드 프로젝트 폴더 중 res/values
폴더 하위에 dimens.xml
파일을 생성하고, 아래와 같이 기재한다.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- size -->
<dimen name="size">142px</dimen>
</resources>
이 수치값을 가져오는 자바 코드에서는 아래와 같은 방법으로 픽셀값을 현재 디바이스에 알맞는 DIP 값으로 불러 사용하면 된다.
// returns the number of pixels for 123.4dip
int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) 123.4, getResources().getDisplayMetrics());
The following units of measure are supported by Android
- Density-independent Pixels (dp, dip)
- An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.
- Scale-independent Pixels (sp)
- This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
- Points (pt)
- 1/72 of an inch based on the physical size of the screen.
- Pixels (px)
- Corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices; each devices may have a different number of pixels per inch and may have more or fewer total pixels available on the screen.
- Millimeters (mm)
- Based on the physical size of the screen.
- Inches (in)
- Based on the physical size of the screen.