본문 바로가기
2022/ANDROID

[ANDROID STUDIO] 레이아웃 (리니어 레이아웃)

by Or0i쿠 2022. 10. 5.

레이아웃 

  • ViewGroup 클래스로부터 상속 받으며 내부에 무엇을 담는 용도로 사용
  • 레이아웃 중에서 가장 많이 사용되는 것은 리니어레이아웃(LinearLayout)

레이아웃에서 자주 사용되는 속성

  • orientation : 레이아웃 안에 배치할 위젯의 수직 또는 수평 방향을 설정
  • gravity : 레이아웃 안에 배치할 위젯의 정렬 방향을 좌측, 우측, 중앙으로 설정
  • padding : 레이아웃 안에 배치할 위젯의 여백을 설정
  • layout_weight : 레이아웃이 전체 화면에서 차지하는 공간의 가중값을 설정, 여러 개의 레이아웃이 중복될때 주로 사용
  • baselineAligned : 레이아웃 안에 배치할 위젯을 보기 좋게 정렬

레이아웃의 종류

  • 리니어레이아웃 : 왼쪽 위부터 아래쪽 또는 오른쪽으로 차례로 배치
  • 렐러티브레이아웃 : 위젯 자신이 속한 레이아웃의 상하좌우의 위치를 지정하 여 배치
  • 테이블레이아웃 : 위젯을 행과 열의 개수를 지정한 테이블 형태로 배열
  • 그리드레이아웃 : 테이블레이아웃과 비슷하지만, 행 또는 열을 확장하여 다 양하게 배치할 때 더 편리
  • 프레임레이아웃 : 위젯들을 왼쪽 위에 일률적으로 겹쳐서 배치하여 중복해서 보이는 효과를 냄

 

레이아웃의 종류

리니어 레이아웃

orientation 속성

  • 리니어레이아웃의 가장 기본적인 속성
  • Vertical : 리니어레이아웃 안에 포함될 위젯의 배치를 수직방향으로 쌓음
  • Horizontal : 수평 방향으로 쌓겠다는 의미

orientation 속성이 vertical 값인 XML 코드

orientation 속성이 horizontal 값인 XML 코드

gravity 속성

  • gravity 속성은 레이아웃 안의 위젯을 어디에 배치할 것인지를 결정

layout_gravity 속성

  • layout_gravity는 자신의 위치를 부모의 어디쯤에 위치시킬지를 결정

baselineAligned 속성

  • baselineAligned 속성은 크기가 다른 위젯들을 보기 좋게 정렬함
  • true와 false 값을 가질 수 있음

중복 리니어레이아웃

  • 형태

 

 

BMI 계산기 키와 몸무게를 입력받고 비만도 출력받아보자.

layout

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

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="63dp"
        android:text="👀 비만도 측정 👀 "
        android:textSize="25dp"
        android:textColor="#9400D3"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="몸무게" />

    <EditText
        android:id="@+id/Edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Kg" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="키" />

    <EditText
        android:id="@+id/Edit2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="cm" />

    <Button
        android:id="@+id/BtnCalc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="bmi 계산" />

    <TextView
        android:id="@+id/TextResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="bmi =" />
</LinearLayout>​

main

package com.example.bmi_a;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
//1. 위젯 변수 계산 및 이용할 변수 선언 (176p 참조)
    EditText edit1, edit2; //입력 위젯 변수선언
    Button btnCalc; //버튼 위젯 변수
    TextView textResult; //계산 결과 보여줄

    String weight, height; //edittext로 입력받은 값 저장할 변수
    Double result; // bmi수치 저장할 변수

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bmi);
        setTitle("✔bmi계산 | 2121042");
        //2. id 찾기 (177p 참조)
        edit1 =(EditText) findViewById(R.id.Edit);
        edit2 =(EditText) findViewById(R.id.Edit2);
        btnCalc=(Button) findViewById(R.id.BtnCalc);
        textResult =(TextView) findViewById(R.id.TextResult);

        //3. 기능 부여
        btnCalc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                weight = edit1.getText().toString();
                height = edit2.getText().toString();
                //bmi=몸무게/키*키
                result = Double.parseDouble(weight) / ((Double.parseDouble(height) / 100) * (Double.parseDouble(height) / 100));
                //bmi값 구하기
                if(result >=30){
                    textResult.setText("계산결과 = "+ result.toString() +" 고도비만");
                }
                else if(result >=25){
                    textResult.setText("계산결과 = "+ result.toString() +" 비만");
                }
                else if(result >=23){
                    textResult.setText("계산결과 = "+ result.toString() +" 과체중");
                }
                else if(result >=18.5){
                    textResult.setText("계산결과 = "+ result.toString() +" 정상");
                }
                else {
                    textResult.setText("계산결과 = "+ result.toString() +" 저체중");
                }


            }
        });
    }
}

main
결과

layout

<?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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="63dp"
        android:text="📃 성적 계산 📃 "
        android:textSize="25dp"
        android:textColor="#9400D3"
        />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="과목1" />

    <EditText
        android:id="@+id/Edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="점" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="과목2" />

    <EditText
        android:id="@+id/Edit2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="점" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="과목3" />

    <EditText
        android:id="@+id/Edit3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="점" />

    <Button
        android:id="@+id/BtnCalc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="성적 계산" />

    <TextView
        android:id="@+id/TextResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="총점 =" />

    <TextView
        android:id="@+id/TextResult2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="평균 = " />

    <TextView
        android:id="@+id/TextResult3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="등급 = " />

 

main

package com.example.sungjuck;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    EditText edit1, edit2, edit3; //입력 위젯 변수선언
    Button btnCalc; //버튼 위젯 변수
    TextView textResult; //계산 결과 보여줄
    TextView textResult2;
    TextView textResult3;

    String score1, score2, score3; //edittext로 입력받은 값 저장할 변수
    Double result; //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sungjuk);
        setTitle("✔성적계산 | 2121042");
        //2. id 찾기 (177p 참조)
        edit1 =(EditText) findViewById(R.id.Edit);
        edit2 =(EditText) findViewById(R.id.Edit2);
        edit3 =(EditText) findViewById(R.id.Edit3);
        btnCalc=(Button) findViewById(R.id.BtnCalc);
        textResult =(TextView) findViewById(R.id.TextResult);
        textResult2 =(TextView) findViewById(R.id.TextResult2);
        textResult3 =(TextView) findViewById(R.id.TextResult3);

        btnCalc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                score1 = edit1.getText().toString();
                score2 = edit2.getText().toString();
                score3 = edit3.getText().toString();

                result = Double.parseDouble(score1)+Double.parseDouble(score2)+Double.parseDouble(score3);
                textResult.setText("총점 = "+result.toString());
                result= (double) result/3.0;
                textResult2.setText("평균 = "+result.toString());

                if(result >=95){
                    textResult3.setText("등급 = a");

                }
                else if(result >=90){
                    textResult3.setText("등급 = b");
                }
                else if(result >=80){
                    textResult3.setText("등급 = c");
                }
                else if(result >=65){
                    textResult3.setText("등급 = d");
                }
                else if(result >=50) {
                    textResult3.setText("등급 = e");
                }
                else {
                    textResult3.setText("등급 = f");
                }


            }
        });

    }
}​

결과

'2022 > ANDROID' 카테고리의 다른 글

[Android Studio] ex) Webview  (0) 2022.11.24
오늘은 망했다  (2) 2022.10.05