본문 바로가기

자린고비 독학하기/자린고비 코딩하기

Android Studio - Relative Layout

반응형

 


<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="URL: "
        android:id ="@+id/lblURL"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"

        />


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etURL"
        android:layout_toRightOf="@+id/lblURL"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="취소 버튼"
        android:id="@+id/btnCancel"
        android:layout_below="@+id/etURL"
        android:layout_alignRight="@id/etURL"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        android:id="@+id/btnCommit"
        android:layout_toLeftOf="@id/btnCancel"
        android:layout_alignTop="@id/btnCancel"

        />

</RelativeLayout>

 

 

  • Relative Layout에 padding 값만 16dp 주고 위젯 추가.
  • layout_to**Of / align** 를 통해서 기존 위젯 값의 왼쪽 / 오른쪽에 놓거나  버튼 위치를 맞출 수 있다.

 

상단 코드의 결과

 

 

 

 

 


<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/linearLayout1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="URL"
            android:id="@+id/textView"
            />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText" />


    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/linearLayout2"
        android:layout_below="@id/linearLayout1"
        android:gravity="right" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="취소 버튼"
            />



    </LinearLayout>





</RelativeLayout>

 

  • Relative Layout 안에 Linear Layout 넣고, padding 16dp 주고 위젯 추가.
  • 상단 결과보다 조금 더 깔끔하다. 

 

 

반응형