Android
RelativeLayout 대비 ConstraintLayout 사용법
aucd29
2018. 4. 27. 09:39
참고링크 : https://academy.realm.io/kr/posts/constraintlayout-it-can-do-what-now/
새로운 코드에 약간 보수적으로 접근하기 때문에 일단은 보류 해두었다가 요즘 제법 사용이 되가는 것 같아
ConstraintLayout 이 RelativeLayout 대비 어떤식으로 명령이 대비 되는지 정리해 본다.
(일단 RelativeLayout 대비 렌더링이 좀더 빠르다고 하는데 +_+ 솔깃했다.., 근데 Linear 나 Frame 대비 했을때 늦겠지? 라고 생각했는데 아니네!!
https://medium.com/@krpiotrek/constraintlayout-performance-c1455c7984d7 )
RelativeLayout를 기준으로 ConstraintLayout 를 변경 한다면
layout_centerInParent =
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
layout_centerHorizontal =
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
layout_centerVertical =
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
// 특이점으로 긴문장을 가지는 TextView 를 layout_centerHorizontal 형태로 설정한 뒤 start / end 에 margin 을 주려면 반드시 width 값을 0dp 로 설정해야 제대로 margin 값이 동작 한다.
layout_below =
app:layout_constraintTop_toBottomOf="@id/상위뷰아이디"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
layout_toEndOf =
app:layout_constraintLeft_toRightOf="@+id/왼쪽에 기준이 되는 뷰아이디"
// 근데 구글 이놈들은 left / right 때문에 Rtl 지원을 위해 Start / End 로 바꿔 놓곤 신규 레이아웃에 또 Left / Right 를 만들어 놓은 이유는 멀까?
(왼쪽 하단)
layout_alignParentBottom =
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
(오른쪽 하단)
layout_alignParentEnd & layout_alignParentBottom =
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
(상위View 를 참조하고 그 밑으로 match_parent 해야할때)
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/상위view_id"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
특정 비율에 맞게 위치 & 비율크기로 하려면
app:layout_constraintWidth_percent=".75"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
대충 이정도만 되도 큰 문제 없이 기존의 RelativeLayout 에서 ConstraintLayout 으로 전환 가능 하며
특이점으로는 RecyclerView 를 이용시에는 아래와 같이 inflate 하면 된다.
View view = LayoutInflater.from(getContext()).inflate(resid, parent, false);
parent 를 null 로 하면 뜨거운 맛을? 보는게 아니고 정렬이 안된다. ㅎ
추가적으로 goneMarin 같은거 있는데 기존에 gone 말고 invisible 했으면 될 일을 gone 해서 문제가 생긴 걸로 생각되니 그게 꼭 필요한건가? 싶기도 하다.
gone == 완전 뿅 사라지는
invisible == 영역은 보전하되 보이질 않는
ps. ConstraintLayout 이 xml 에서 사용하기 좀 지저분해보이는 것이 있기 때문에 wrapping 해서 기존의 relativelayout 의 attribute 형태로 사용해보는것도 나쁘지 않겠다 ? 라는 생각이 들었다.
새로운 코드에 약간 보수적으로 접근하기 때문에 일단은 보류 해두었다가 요즘 제법 사용이 되가는 것 같아
ConstraintLayout 이 RelativeLayout 대비 어떤식으로 명령이 대비 되는지 정리해 본다.
(일단 RelativeLayout 대비 렌더링이 좀더 빠르다고 하는데 +_+ 솔깃했다.., 근데 Linear 나 Frame 대비 했을때 늦겠지? 라고 생각했는데 아니네!!
https://medium.com/@krpiotrek/constraintlayout-performance-c1455c7984d7 )
RelativeLayout를 기준으로 ConstraintLayout 를 변경 한다면
layout_centerInParent =
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
layout_centerHorizontal =
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
layout_centerVertical =
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
// 특이점으로 긴문장을 가지는 TextView 를 layout_centerHorizontal 형태로 설정한 뒤 start / end 에 margin 을 주려면 반드시 width 값을 0dp 로 설정해야 제대로 margin 값이 동작 한다.
layout_below =
app:layout_constraintTop_toBottomOf="@id/상위뷰아이디"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
layout_toEndOf =
app:layout_constraintLeft_toRightOf="@+id/왼쪽에 기준이 되는 뷰아이디"
// 근데 구글 이놈들은 left / right 때문에 Rtl 지원을 위해 Start / End 로 바꿔 놓곤 신규 레이아웃에 또 Left / Right 를 만들어 놓은 이유는 멀까?
(왼쪽 하단)
layout_alignParentBottom =
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
(오른쪽 하단)
layout_alignParentEnd & layout_alignParentBottom =
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
(상위View 를 참조하고 그 밑으로 match_parent 해야할때)
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/상위view_id"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
특정 비율에 맞게 위치 & 비율크기로 하려면
app:layout_constraintWidth_percent=".75"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
대충 이정도만 되도 큰 문제 없이 기존의 RelativeLayout 에서 ConstraintLayout 으로 전환 가능 하며
특이점으로는 RecyclerView 를 이용시에는 아래와 같이 inflate 하면 된다.
View view = LayoutInflater.from(getContext()).inflate(resid, parent, false);
parent 를 null 로 하면 뜨거운 맛을? 보는게 아니고 정렬이 안된다. ㅎ
추가적으로 goneMarin 같은거 있는데 기존에 gone 말고 invisible 했으면 될 일을 gone 해서 문제가 생긴 걸로 생각되니 그게 꼭 필요한건가? 싶기도 하다.
gone == 완전 뿅 사라지는
invisible == 영역은 보전하되 보이질 않는
ps. ConstraintLayout 이 xml 에서 사용하기 좀 지저분해보이는 것이 있기 때문에 wrapping 해서 기존의 relativelayout 의 attribute 형태로 사용해보는것도 나쁘지 않겠다 ? 라는 생각이 들었다.