Android Set width as a percent of parent width

사용 버전: Android Studio 4.1.2
사용 언어: Kotlin 1.4.21


안드로이드 Set width as a percent of parent width를 해보겠습니다.

layout에서 parent의 크기의 1/2(50%)나 1/3(30%) 정도로 크기를 조절하고 싶을 때가 있습니다.
이럴 때에 어떻게 해야 하는지 알아보겠습니다.

LinearLayout을 사용합니다.





이 상태로 실행해 봅시다.




두 개의 글자가 위에 옹기종기 붙어 있는 것이 보입니다.




위에 Profile 영역을 30%로 만들고, 밑의 Profile 영역을 70%로 만들겠습니다.

LinearLayout에 weightSum을 줍니다.

weightSum은 100% 값이라고 보면 됩니다.







android:layout_height는 0dp로 변경합니다.

나머지 TextView에는 layout_weight를 각각 퍼센트에 맞게 설정합니다.

1이 100%이기 때문에 0.3(30%), 0.7(70%)이 됩니다.




실행해 봅시다.





각 영역이 설정한 %에 맞게 변경되었습니다.



끝.


카테고리: Android


댓글

이 블로그의 인기 게시물

Python urllib.parse.quote()

KiCad 시작하기 4 (기존 회로도 수정 및 추가)

Android Compose Coil library

KiCad 시작하기 1 (회로도 만들기)

Android Notification with Full Screen

iOS Swift callAsFunction

Forensics .pyc 파일 .py로 복구하기

iOS Error Undefined symbol Testing.Trait

Android Custom IME(Input method editor) 만들기

Android Compose automation for getting localized images to use on Play Store app image