본문 바로가기
프로그래밍 놀이터/안드로이드, Java

[android] PercentLayout

by 돼지왕 왕돼지 2019. 1. 25.
반응형

[android] PercentLayout


https://blog.stylingandroid.com/percent-part-1

LayoutParams, linearlayout weight vs percentlayout, nested weight, percentframelayout, percentlayout bug, percentrelativelayout, support lib, [android] PercentLayout


-

LinearLayout 의 weight 로 해결할 수 있는데 왜 PercentLayout 이 필요할까?


    parent 가 LinearLayout 이 아닌데 percent 기준으로 view 의 layout 하거나 size 를 구성할 일이 있을 것이다.

    nested weight 는 cost 가 꽤 크지만(weight 자체가 2번 measure), PercentLayout 은 그렇지 않다.



-

PercentLayout 은 2가지 타입이 있다. PercentRelativeLayout 과 PercentFrameLayout

기본적인 동작은 RelativeLayout 과 FrameLayout 과 같다. 그러나 LayoutParams 에 비율(percent)에 관련된 요소들이 추가되어 있다.



-

PercentLayout 사용을 위해서는 support lib dependency 를 추가해야 한다.

compile 'com.android.support:percent:23.0.0’



-

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity”> <!— layout_width, layout_height 는 필요 없다(mandatory 가 아니다) —> <View android:id="@+id/first" android:background="@color/sa_green_dark" app:layout_heightPercent="50%" app:layout_marginLeftPercent="25%" app:layout_marginTopPercent="25%" app:layout_widthPercent="50%" /> <!— width 를 0dp 로 했지만 align 으로 인해 자동으로 width 가 정해진다 —> <View android:layout_width="0dp" android:layout_height="32dp" android:layout_alignLeft="@id/first" android:layout_alignStart="@id/first" android:layout_alignRight="@id/first" android:layout_alignEnd="@id/first" android:layout_below="@id/first" android:layout_marginTop="8dp" android:background="@color/light_grey" /> </android.support.percent.PercentRelativeLayout>



-

"wrap_content option 을 주면.. percent 규칙을 먼저 적용해 size 를 구하고, 

만약 wrap_content 가 percent 적용된 size 보다 크다면 wrap_content 규칙을 따르게 된다.” 고 

document 에 써 있지만.. 실제 test 결과로는 그렇지 않았다고 한다. ( -> 최신 버전에서는 어떻게 될지 모르겠다. )


추가로 percent 가 적용된 값이 원하는데로 동작하지 않는 이슈도 있다고 하는데 버그인지 모르겠다. ( 현재 고쳐졌는지 모르겠다. )




반응형

댓글