반응형

안드로이드 1일차 기본적인 레이아웃들과  자바 리스너 등을 알아보았다
XML은 안드로이드 애플리케이션에서 레이아웃같은리소스를만드는데쓰인다.
자바 코드는 안드로이드 어플에서 직접 동작되는 부분을정의하는 데 쓰이는데, 요즘은 코틀린으로 대체된다.
액티비티란 안드로이드 화면을 나타내는 단위로 레이아웃+자바 코드이다
액티비티 간의 이동은 intent라는 것을쓴다
대표적인 레이아웃 3가지를 설명하겠다.
relativelayout, constraintlayout, linearlayout
첫 번째 relativelayout은 가장 많이 쓰이는 레이아웃으로 부모관계 덕에 훨씬 편하게 UI를 짤 수 있게 되었다.
두 번째 constraintlayout은 반응형 앱 때문에 나온 것으로 초보자들이 하기에는 어려움이 있다.
마지막 linearlayout은 가로, 세로 기준을 잡고 비율을 정할 수 있는 레이아웃이다.

 

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="center"
android:id="@+id/center"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="left"
android:layout_toLeftOf="@+id/center"
android:id="@+id/left"
android:layout_centerVertical="true"

/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/center"
android:layout_centerVertical="true"
android:text="right"
android:id="@+id/right"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/center"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/center"
android:text="above"
android:id="@+id/above"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/center"
android:layout_alignLeft="@+id/center"
android:text="below"
android:id="@+id/below"
/>

android:layout_centerHorizontal="true"
이것은 가로 가운데를 정해주는 것,
android:layout_centerVertical="true"

이것은 세로 가운데를 정해주는 것이다.
android:text="center"
이것은 말 그대로 텍스트를 정해주는 것
android:id="@+id/center"
이것은 버튼에 아이디를 정해주는 것
android:layout_toLeftOf="@+id/center"
이것은 센터라는 아이디를 가진 레이아웃 왼쪽에 위치하겠다는 것이다
android:layout_above="@+id/center"
이것은 센터라는 아이디를 가진 레이아웃 위쪽에 위치하겠다는 것이다
android:layout_alignLeft="@+id/center"
이것은 센터가 가진 왼쪽 거리만큼 자기도 거리를 가지겠다는 것이다 android:layout_below="@+id/center"
이것은 센터라는 아이디를 가진 레이아웃 밑 쪽에 위치하겠다는 것이다

package com.example.ghj52.test1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private Button center;
    private Button left;
    private Button right;
    private Button above;
    private Button below;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        setListner();
    }
    private void init(){
        center = (Button) findViewById(R.id.center);
        left = (Button) findViewById(R.id.left);
        right = (Button) findViewById(R.id.right);
        above = (Button) findViewById(R.id.above);
        below = (Button) findViewById(R.id.below);
    }
    private void setListner(){
        center.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"센터!",Toast.LENGTH_LONG).show();
            }
        });
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"레프트!",Toast.LENGTH_LONG).show();
            }
        });
        right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"라이트!",Toast.LENGTH_LONG).show();
            }
        });
        above.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"위!",Toast.LENGTH_LONG).show();
            }
        });
        below.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),"밑!",Toast.LENGTH_LONG).show();
            }
        });
    }

}

먼저, 메인 클래스 안에서 변수 선언을 해주고, 리스트라는 메서드를 만들어 
xml 파일에 있는 center, left, right, below, above라는 아이디를 가진 레이아웃들을 찾아 자바 변수와 연결(?) 시켜준다. 리스너를 만드는데 리스너란,
자극이 오면 바로 대응할 수 있게 계속 기다리고 있는 것이다. 클릭을 했을 때
실행할 것들을 정해준다 그래서 클릭을 했을 때 토스트 메시지를 띄우게 만든 것이다. 그리고 만든 메서드 2개를 onCreate라는 메서드에 호출시킨다.
onCreate는 메인 메서드라고 생각하면 된다.

반응형

'안드로이드 > APK 개발' 카테고리의 다른 글

안드로이드 기본 (2)  (0) 2022.03.11