본문 바로가기

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

Android Studio - Linear Layout: 위젯 추가

반응형

 

 

activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="첫번째 버튼"
        android:id="@+id/button1"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="두번째 버튼"
        android:id="@+id/button2"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="세번째 버튼"
            android:id="@+id/button3"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="네번째 버튼"
            android:id="@+id/button4"
            android:layout_weight="1"
            />


    </LinearLayout>
</LinearLayout>

 

 

 

  • match_parent : 부모 컨테이너가 가지는 길이와 같게 해 준다
  • wrap_content: 필요한 길이만 사용한다

 

  • @+id : 새로 선언하는 리소스 아이디를 등록하겠다 
  • @id: 기존에 사용하고 있던 리소스 아이디를 참조하겠다

 

 

Linear Layout: vertical

 

 

Linear Layout 페이지에 버튼을 두 개 추가하였다. 

 

 

 


 

 

가로 / 세로 값을 전부 부모 값과 일치시켰다.

 

화면 전체가 채워져서 다른 요소들은 보이지 않는다.

 

 


 

 

Linear Layout: horizental

 

 

Linear Layout 안에 또 다른 Linear Layout을 만들어서 버튼 두 개를 추가하였다. 두 버튼 다 height가 match_parent이다.

 

 

네 번째 버튼이 세 번째 버튼에 의해 가려져서 보이지 않는다. 

 

 

 

 


 

 

wrap content로 바꿔 주었다.

 

버튼이 둘 다 잘 보인다. 

 

 

 


 

 

세 번째 버튼은 wrap_content, 네 번째 버튼은 match_parent 값을 지정하였다. 

 

 

세 번째 버튼이 채우고 남은 부분을 네 번째 버튼이 채운다.

 

 


 

 

버튼을 전부 wrap_content로 지정하고, 비율 지정을 위해 layout_weight를 지정해 주었다.

 

 

같은 값을 지정해 주어서 같은 비율로 출력된다.

 

 

 

반응형