ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 화면 2등분된 레이아웃
    android 2021. 3. 22. 13:38
    728x90

    화면 2등분

     

    1. LinearLayout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <LinearLayout
            android:background="#00701a"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
        </LinearLayout>
        <LinearLayout
            android:background="#76d275"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
        </LinearLayout>
    </LinearLayout>

    android:weight="1"

    을 사용하여 차지하는 범위를 반반으로 설정한다.

     

    LinearLayout 을 사용하면 세로운 뷰를 추가하면 레이아웃이 깨질 수 있으므로

     

    더욱 유연한 Layout을 원한다면 RelativeLayout을 사용할 것을 추천한다.

     

    2. RelativeLayout

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <View
            android:id="@+id/strut"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_centerVertical="true"/>
        <RelativeLayout
            android:background="#00701a"
            android:layout_alignParentTop="true"
            android:layout_alignBottom="@id/strut"
            android:layout_width="match_parent"
            android:layout_height="0dp">
        </RelativeLayout>
        <RelativeLayout
        	android:background="#76d275"
            android:layout_alignParentBottom="true" 
            android:layout_alignTop="@id/strut"               
            android:layout_width="match_parent"
            android:layout_height="0dp">
        </RelativeLayout>
    </RelativeLayout>

    LinearLayout 보다 조금 불편할 수 있지만 조금더 유연하게 사용할 수 있는 방법이다.

     

    1. 먼저 가운데에 위치 시켜줄 <View>를 하나 생성한다.

    <View
            android:id="@+id/strut" <- 기준으로 하기위해 id 설정 필수
            android:layout_width="0dp" <- 화면에 보이지 않도록 0으로
            android:layout_height="0dp" <- 화면에 보이지 않도록 0으로
            android:layout_centerVertical="true"/> <- 가운데 위치 시키기
            android:layout_centerHorizontal="true" <- 세로를 기준으로 가운데 위치 시키기

     

    2. RelativeLayout을 기준으로 생성한 View를 기준으로 반반을 나눈다.

    <RelativeLayout
            android:background="#00701a"
            android:layout_alignParentTop="true" <- 부모 뷰를 기준으로 위쪽 고정
            android:layout_alignBottom="@id/strut" <- 위에서 설정한 view를 기준으로 아래쪽 고정
            android:layout_width="match_parent"
            android:layout_height="0dp">
        </RelativeLayout>
        <RelativeLayout
            android:layout_alignTop="@id/strut" <- 위에서 설정한 view를 기준으로 위쪽고정
            android:layout_alignParentBottom="true" <- 부모를 기준으로 아래쪽 고정
            android:background="#76d275"
            android:layout_width="match_parent"
            android:layout_height="0dp">
        </RelativeLayout>
    반응형

    'android' 카테고리의 다른 글

    Basic Activity 분석 - 1.뷰 결합  (0) 2021.12.10
    Java/Android Google Sheets 사용하기  (0) 2021.12.05
    android Ripple Effect  (0) 2021.03.24
    안드로이드 라운드 버튼 만들기  (0) 2021.03.22
    안드로이드 원형 버튼 그리기  (0) 2021.03.22

    댓글

Designed by Tistory.