관리 메뉴

드럼치는 프로그래머

[안드로이드] Android PercentLayout 본문

★─Programing/☆─Android

[안드로이드] Android PercentLayout

드럼치는한동이 2018. 2. 27. 11:36

[출처] http://www.kmshack.kr/2015/08/android-percentlayout/


해당 URL의 소중한 자료 정독 후 프로그래밍 학습에 도움이 되었음을 밝힙니다.



안드로이드의 차기버전인 6.0(M)의 공식 명칭이 “마시멜로”로 정해졌다. 지난 구글 I/O를 통해 안드로이드M 개발자 프리뷰 버전을 공개후 많은 피드백을 바탕으로 최종개발자 프리뷰 버전과 안드로이드 6.0 공식 SDK가 공개 되었다. API Level 23을 타겟으로 앱을 빌드하여 구글 플레이에 앱을 출시 할 수 있다.

마시멜로 SDK 공개와 동시에 서포트라이브러리도 23버전으로 업데이트 되었으며, 마시멜로에서 추가된 기능을 하위호환하기 위한 것으로 판단된다.

그리고 이외에도 이 외에도 customtabs, percent, recommendation, preference-v7, preference-v14, preference-leanback-v17 등의 라이브러리도 함께 추가되었다. 그중 percent 라이브러리에 대해 알아보자.

percent 라이브러리는 View와 관련된 라이브러리로 2가지의 레이아웃을 포함하고 있다.

  • PercentFrameLayout
  • PercentRelativeLayout

기존의 안드로이드의 레이아웃은 높이또는 넓이를 고정된 값으로 정해주었다면 이 percent 라이브러리의 레이아웃은 디바이스의 크기에 따라 레이아웃의 크기가 변하게 된다. 퍼센테이지에 따라 레이아웃은 높이또는 넓이의 크기가 변한다.

DPI개념인 수치에 따라 물리적인 크기가 같은 경우의 단점을 보완할 수 있도록 배려해주는 레이아웃이다. 즉 폰에서의 버튼을 사용자가 잘 누를 수 있도록 개발을 했더니 큰화면의 테블릿 화면에서는 화면에 비해 버튼이 작아 공간의 낭비 또는 디자인적으로 문제가 될 수 있는 문제점을 해결 해줄 가능성이 크다.

안드로이드 5.0에서 발표된 머트리얼 디자인을 적용하기위해서도 이러한 레이아웃을 필요로하기때문에 안드로이드팀에서 고민을 해왔을 것으로 생각된다.

그렇다면 이제 한번 어떻게 사용하는지 한번 알아보자. 먼저 안드로이드 스튜디오에서 Gradle에 라이브러리를 추가하자.

complie 'com.android.support:percent:23.0.0'

레이아웃 속성들은 둘다 동일하며 이름만 봐도 알 수 있기 때문에 따로 설명 하지 않겠다.

  • heightPercent
  • widthPercent
  • marginBottomPercent
  • marginEndPercent
  • marginLeftPercent
  • marginPercent
  • marginRightPercent
  • marginStartPercent
  • marginTopPercent

모든 수치는 퍼센트단위로 작성하면 되며 아래와 같이 간단하게 레이아웃을 한번 만들어 보자.

<android.support.percent.PercentFrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.percent.PercentFrameLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:background="#ff44aacc"
        app:layout_heightPercent="50%"
        app:layout_widthPercent="50%">

        <android.support.percent.PercentFrameLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:background="#ffcc5ec7"
            app:layout_heightPercent="50%"
            app:layout_widthPercent="50%">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:background="#ff7ecc16"
                android:gravity="center"
                android:text="margin 15% of w"
                />

        </android.support.percent.PercentFrameLayout>

    </android.support.percent.PercentFrameLayout>

    <TextView android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_gravity="bottom|right"
        android:background="#44ff0000"
        android:gravity="center"
        android:text="15%w,15%w"
        app:layout_heightPercent="15%"
        app:layout_marginPercent="5%"
        app:layout_widthPercent="15%"/>


</android.support.percent.PercentFrameLayout>

디바이스 크기에 따라 작성한 퍼센트로 레이아웃의 크기가 잡히는 것을 볼 수 있다. 이렇게 디바이스 크기의 퍼센테이지에 따른 레이아웃의 크기가 결정되는 기능을 어디에 사용 할수 있을까라는 생각을 해볼 수 있다.

머티어리얼 디자인을 보면 테블릿의 경우 넓은 화면을 모두 활용 하지 못하는 경우 작은 창을 띄워서 사용하는 가이드를 볼 수 있을 것이다. 이런 디자인가이드에 가장 잘 대응 할 수 있을것으로 판단된다.


폰보다 테블릿이나 TV에 유용하게 사용될것으로 판단되며, 이런 세세한 부분까지 레이아웃을 지원하는 것을 보면 안드로이드의 다음버전인 “마시멜로”는 폰을 포함 테블릿, TV에 대한 플랫폼 확장을 해나가는 면모를 볼 수 있다.


Comments