[Android] STUDY 3 - My02_LinearLayout

PSEveloper ㅣ 2020. 11. 19. 02:03

 

 

 

1. ConstraintLayout에서 LinearLayout 으로 바꾸는 방법

 


1) design 탭에서 바꾸기

  ConstraintLayout 마우스오른쪽 convert layout 클릭한 후 바꾸기
   → 왼쪽 상단 아이콘으로 vertical로 바꾸기

 

 


2) code에서 바꾸기

         <LinearLayout

                 xmlns:android="http://schemas.android.com/apk/res/android"

                  android:layout_width="match_parent"

                  android:layout_height="match_parent"

                  android:orientation="vertical">

        </LinearLayout>




2. 화면분할


감싸고 있는 LinearLayout 코드에 android:weightSum="분할 하고자 하는 자식 레이아웃의 수"
자식 레이아웃 코드에 android:layout_weight="1" 추가한 후,
LinearLayout이 vertical인 경우, 자식 레이아웃 코드의 android:layout_height 0dp로 준다.
LinearLayout  horizontal인 경우, 자식 레이아웃 코드의 android:layout_width 0dp로 준다.

 

 

 

 

● activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="3">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#81BD97"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:weightSum="30">

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="15"
            android:text="1.5"/>

        <Button
            android:id="@+id/btnTest"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:text="0.5"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:text="1의 비율"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#D3CC87"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:weightSum="3">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1대"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="2의 비율"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="#C180CF"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:weightSum="3">
        <Button
            android:background="@mipmap/ic_launcher_round"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:layout_weight="1"
            android:weightSum="4">
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>
        </LinearLayout>

        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

 ① <LinearLayout>을 3개 작성한다.

 ② width, height, orientation, weightsum 설정으로 화면을 세 개로 분할한다.

 ③ background로 배경색을 넣는다.

 ④ 버튼을 만들고 버튼의 width, height, weight를 설정한다.

 ⑤ layout_gravity 설정으로 버튼의 위치를 조절한다.

 ⑥ background와 imageView에 이미지를 넣는다.

 

 

● MainActivity.java

package com.example.my02_linearlayout;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageView = findViewById(R.id.imageView); //변수를 선언하고 id를 찾는다.
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "이미지뷰가 클릭!!!", Toast.LENGTH_LONG).show();
            }
        });
    }
}