IT/Android

[안드로이드] 화면 해상도에 관계없는 레이아웃(Layout) 만들기 펌 ) http://snowbora.com/422

데브렉스 2012. 2. 8. 15:02
반응형
펌 ) http://snowbora.com/422

픽셀 좌표인 px를 사용해서 값을 지정해보기도 했고,
밀도 좌표인 dip나 dp를 사용해보기도 했습니다.
하지만 !!!
이렇게 px나 dip만을 이용해서 모든 해상도를 고려한 레이아웃이 술술술 나온다면 얼마나 좋겠습니까?
저도 갤럭시S2와 갤럭시탭7인치를 갖고 개발하다가 큰 난관에 봉착하게 되었는데
이 녀석들이 같은 dpi를 가지고 있는 겁니다. 화면 해상도는 480x800과 600x1024인데 dpi는 둘 다 240dpi입니다.

즉, 갤럭시S2에 맞게 GUI를 구성하면 갤럭시탭에서 깨진 GUI를 보여주게 되더군요.


결국, 이런 저런 문제 해결을 고려하다가 레이아웃에서 가중치(weight)값을 이용해서 화면을 구성하는 방법을 택하게 되었습니다.
어제 하루종일 px와 dip를 갖고 고민을 했지만, weight를 이용하니 고민이 한번에 날아갔습니다. +_+


만약 이와 같은 GUI를 구성한다고 할 때, 레이아웃은 다음과 같이 작성하면 됩니다.
아무 것도 없는 공간에 LinearLayout을 이용해서 공간을 할당하는게 키포인트입니다.


샘플 xml 코드는 다음과 같습니다. 

<?xml version="1.0" encoding="utf-8"?>

   

<LinearLayout

android:orientation="horizontal"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:background="@drawable/player_landscape_bg_control">


    <LinearLayout  

        android:layout_width="0px" 

        android:layout_height="fill_parent"

        android:layout_weight="175"/>

   

<ImageButton

android:id="@+id/btnImgP_ZoomOut"

android:layout_width="0px" 

            android:layout_height="wrap_content"

            android:layout_weight="83"

            android:background="@drawable/photoviewer_btn_zoom_out_landscape"/> 

    

    <LinearLayout  

       android:layout_width="0px" 

       android:layout_height="fill_parent"

       android:layout_weight="40"/>


    <ImageButton

android:id="@+id/btnImgP_PlayAndPause"

android:layout_width="0px" 

         android:layout_height="wrap_content"

         android:layout_weight="83"

         android:background="@drawable/photoviewer_btn_play_landscape"/> 

   

<LinearLayout  

       android:layout_width="0px" 

       android:layout_height="fill_parent"

       android:layout_weight="30"/>


    <ImageButton

android:id="@+id/btnImgP_ZoomIn"

android:layout_width="0px" 

         android:layout_height="wrap_content"

         android:layout_weight="83"

         android:background="@drawable/photoviewer_btn_zoom_in_landscape" />

   

    <LinearLayout  

       android:layout_width="0px" 

       android:layout_height="fill_parent"

       android:layout_weight="40"/>

   

    <ImageButton

android:id="@+id/btnImgP_ChangeDMR"

android:layout_width="0px" 

         android:layout_height="wrap_content"

         android:layout_weight="83"

         android:background="@drawable/photoviewer_btn_changedisplay_landscape" />

   

    <LinearLayout  

       android:layout_width="0px" 

       android:layout_height="fill_parent"

       android:layout_weight="183"/>


</LinearLayout>

샘플은 가로 방향의 빈 공간에 대해서만 가중치를 줬지만 weight는 가로든 세로든 어디에나 적용할 수 있습니다.
반응형