drawable 이미지가 안보일 때에는 이미지명을 영어로 시작해보자

시작이 숫자나 특문일 경우 인식이 안된다

 

-

렐러티브 레이아웃

리니어가 아니기 때문에 오리엔테이션이 필요 없다

 

 

버튼 2개를 추가했으나 겹쳐버린다

 

 

-------------------

 

 

---------------------

 

 

위아래 배치

 

더보기
<?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"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/btn1"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Top Menu" />

    <Button
        android:id="@+id/btn2"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bottom Menu" />

    

</RelativeLayout>

리니어로 위와같이 하려면 마진을 엄청 줘야하고 불편하다

그래서 렐러티브를 이용한다

 

 

드래그하면 코드 위치가 바뀐다

 

 

부모와의 관계

 

 

start 는 시작위치가 위인지 왼쪽인지 상관없이 시작지점

 

 

다른 컴포넌트의 아래에 배치

 

---------------------

 

 

btn2의 위쪽에 배치

 

-----------------

 

 

 

더보기
<?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"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/btn1"
        android:layout_alignParentTop="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Top Menu1" />

    <Button
        android:id="@+id/btn4"
        android:layout_toRightOf="@id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Top Menu2" />

    <Button
        android:id="@+id/btn3"
        android:layout_below="@id/btn1"
        android:layout_above="@id/btn2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Bottom Menu" />

    <Button
        android:id="@+id/btn2"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bottom Menu" />


</RelativeLayout>

---------------

 

 

----------------

 

 

겹쳐버린다

-------------

 

 

렐러티브 레이아웃은 HTML의 div 와 같은 역할 

-----------

 

 

--------------

 

 

 

더보기
<?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"
    tools:context=".MainActivity">


    <RelativeLayout
        android:id="@+id/top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn1"
            android:layout_alignParentTop="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Top Menu1" />

        <Button
            android:id="@+id/btn4"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:layout_toRightOf="@id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Top Menu2" />

    </RelativeLayout>

    <Button
        android:id="@+id/btn3"
        android:layout_below="@id/top"
        android:layout_above="@id/btn2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Bottom Menu" />

    <Button
        android:id="@+id/btn2"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Bottom Menu" />


</RelativeLayout>

------------------

 

리니어 레이아웃 정리

 

더보기를 눌러 전체코드 확인

더보기
<?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"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/button"
        android:layout_weight="1"
        android:gravity="left"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:text="b1" />

    <Button
        android:id="@+id/button2"
        android:layout_weight="1"
        android:gravity="right"
        android:layout_gravity="right"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:text="b2" />

    <Button
        android:id="@+id/button3"
        android:layout_weight="1"
        android:layout_gravity="center"
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:text="b3" />


</LinearLayout>

 

-

 

버튼 내의 글자를 옮기기

 

 

속성을 줄때 id 아래에 주자

gravity로 위치조정

 

 

스마트폰 화면 전체를 window 라고 부른다

window는 백그라운드로 항상 깔려 있다

그 위에 올라가는 것이 activity

하나의 앱에는 여러개의 activity가  있다 (메인 로그인 회원가입 화면 등)

activity 위에는 SurfaceView가 올라간다

앱을 만든다는 것은 SurfaceView라는 하얀도화지에 그림을 그린다

 

뷰 - 그림

위젯 - 이벤트가 달려 있는 그림 - 안드로이드에서 제공하는 그림들 - 버튼 텍스트뷰 등등

머터리얼 - 

 

위젯을 직접만드는 것은 쉽지 않고, 기존의 것을 커스텀하여 사용

 

activity는
(1)자바파일
-로직
(2)XML
-화면

위의 두가지가 onCreate에서 합쳐져서 출력된다

 

자바에서는 함수에 파라미터로 넘기지만 

안드로이드에서는 시리얼 통신을 하여 데이터를 전송

 

------------------

뎁스depth가 있으면 있을수록 느려진다

---------------

애매한 위치에 배치하려면 margin을 줘야한다

---------------

 

 

---------------

 

사진 저장 위치

----------------

 

 

 

 

가중치가 자동으로 잡힌다

 

width를 0으로 하고 height를 match_parent로 설정

 

---------------

 

더보기
<TextView
            android:id="@+id/textView"
            android:textSize="25sp"
            android:textStyle="bold"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="TimeLine" />

-------------------

 

 

 

 

이미지 구성

 

스케일 타입 centerCrop

------------------------------

 

 

클립아트 만들 때 하는 것

---------------------

 

 

 

 

 

 

 

 

 

---------------------

 

 

렐러티브

이미지뷰 텍스트뷰 이미지뷰

 

더보기

 

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


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="TimeLine"
            android:textSize="25sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Untitled"
            android:textSize="25sp"
            android:textStyle="bold" />

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:scaleType="centerCrop"
            app:srcCompat="@drawable/img1" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp">


            <ImageView
                android:id="@+id/imageView3"
                android:layout_marginLeft="10dp"
                android:layout_width="30dp"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                app:srcCompat="@drawable/ic_favorite" />

            <TextView
                android:id="@+id/textView3"
                android:gravity="center"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@id/imageView3"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="TextView" />

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="35dp"
                android:layout_marginRight="10dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                app:srcCompat="@drawable/ic_more" />
        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

 

-----------------------

 

 

 

-------------------

 

 

 

더보기
<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="60dp">

            <TextView
                android:id="@+id/tv_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="아이디"
                android:textStyle="bold"
                android:textSize="25sp">

            </TextView>

            <TextView
                android:id="@+id/tv_comment"
                android:layout_marginLeft="10dp"
                android:layout_toRightOf="@id/tv_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="내용"
                android:textSize="25sp">

            </TextView>

        </RelativeLayout>

 

---------------------

 

 

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


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="TimeLine"
            android:textSize="25sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Untitled"
            android:textSize="25sp"
            android:textStyle="bold" />

    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

<!--        스크롤뷰는 레이아웃을 하나 밖에 못둔다 레이아웃으로 묶기-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <include layout="@layout/layout_content" />
            <include layout="@layout/layout_content" />
            <include layout="@layout/layout_content" />

        </LinearLayout>


    </ScrollView>

</LinearLayout>

-------------------------

 

다시 만들어봄

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


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"

        android:orientation="horizontal">

        <TextView
            android:id="@+id/tv_1"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="20sp"
            android:text="TextView1" />

        <TextView
            android:id="@+id/tv_2"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:textSize="20sp"
            android:text="TextView2" />
    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <include layout="@layout/layout_content" />
            <include layout="@layout/layout_content" />
            <include layout="@layout/layout_content" />
            <include layout="@layout/layout_content" />

        </LinearLayout>

    </ScrollView>




</LinearLayout>

 

layout_content.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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="375dp"
        android:scaleType="centerCrop"
        app:srcCompat="@drawable/tear" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">


        <ImageView
            android:id="@+id/imageView2"
            android:layout_alignParentLeft="true"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/ic_fav" />

        <TextView
            android:id="@+id/textView3"
            android:layout_toRightOf="@id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="TextView" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_alignParentRight="true"
            android:layout_width="40dp"
            android:layout_height="match_parent"
            app:srcCompat="@drawable/ic_more" />


    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">


        <TextView
            android:id="@+id/textView4"
            android:gravity="center"
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textSize="20dp"
            android:text="TextView" />

        <TextView
            android:id="@+id/textView5"
            android:gravity="center"
            android:layout_marginLeft="30dp"
            android:layout_toRightOf="@id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textSize="20dp"
            android:text="TextView" />
    </RelativeLayout>

    <View
        android:background="@android:color/background_dark"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>

</LinearLayout>

 

 

 

 

 

 

---------------

플래그와 부가 데이터

액티비티는 [액티비티 매니저] 라는 객체에 의해 [액티비티 스택]이라는 것으로 관리됨

액티비티를 차곡차곡 쌓아두고 상위에 있는 액티비티가 없어지면 이전의 액티비티가 화면에 보임

순서

액티비티 생성

매니페스트 파일 등록

startActivity() 또는 startActivityForResult() 메서드를 사용해 실행

새로운 액티비티가 화면에 띄워짐

이전 액티비티는 액티비티 스택에 저장

액티비티가 없어지면 스택 최상위에 있는 액티비티가 노출

(동일한 액티비티를 여러번실행하면 스택에 계속 쌓여 데이터를 여러번 접근하거나 리소스 문제 발생)

NO_FLAG

FLAG_ACTIVITY-SINGLE-TOP

FLAG_ACTIVITY-NO_HISTORY

FLAG_ACTIVITY-CLEAR_TOP

 

NO_FLAG 일 경우 같은 액티비티가 계속 해서 쌓인다

FLAG_ACTIVITY-SINGLE-TOP 일 경우 같은 액티비티는 하나만 쌓인다

 

새로운 액티비티를 띄워주는 액티비티를 부모 액티비티라고 할경우

전달하는 인텐트는 새로 만들어진 인텐트의 onCreate()메서드 안에서 getIntent()메서드로 참조 할 수있다

 

onNewIntent()메서드를 재정의 하면 액티비티가 새로 만들어지지 않았을 때 인텐트 객체만 전달 받을 수 있음

액티비티가 이미 메모리에 객체로 만들어져 있다면 액티비티를 다시 띄우더라도 onCreate()가 실행되지 않기 때문에

onNewIntent()를 재정의 해서 사용해야한다

 

브로드캐스트 수신자?

 

FLAG_ACTIVITY-NO_HISTORY

항상 맨처음의 액티비티가 보이게된다 알림 메시지등 한번화면에 보여줄 때 좋음

 

FLAG_ACTIVITY-CLEAR_TOP

는 되돌아갈 때 스택에 상위 액티비티를 다 제거하고 메인 액티비티로 가게됨

 

부가데이터 

Intent Bundle

번들 객체는 해시테이블과 유사

기본자료형을 넣고 뺄수 있지만 Serializable 객체도 사용가능

안드로이드는 객체를 전달할때 Serializable 보다 Parcelable인터페이스 권장

 

 

 

 

 

------------------

 

 

 

MainActivity

더보기
package com.jaybon.lifecycleex02;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "Main_Activity";

    //메인 액티비티의 전역변수, 걸어두고 사용하는 것이 편리하다
    //메인 액티비티의 모든 것이 담겨있다
    private Context mContext = MainActivity.this;

    private Button btnNum, btnEmail;
    private TextView tvNum, tvEmail;

    
    // 콜백함수
    // 어떤화면이 꺼지면 데이터값을 가져오는 것?
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.d(TAG, "onActivityResult: 콜백받음");
    }

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

        tvNum = findViewById(R.id.tv_num);
        tvEmail = findViewById(R.id.tv_email);
        btnEmail = findViewById(R.id.btn_email);
        btnNum = findViewById(R.id.btn_num);

        // 리스너
        btnNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 트럭 생성 getBaseContext() 또는 MainActivity.this 또는 전역변수로 등록하여 사용
                // 출발지 MainActivity.this, 목적지 Sub2Activity.class
                Intent intent = new Intent(mContext, SubActivity.class);

                // startActivity(intent); 이동만 할때 사용

                // 이동 및 응답을 받을 수 있는 것
                // 나를 띄운 페이지 확인 requestCode (enum 등을 사용하자)
                startActivityForResult(intent, 1);

            }
        });
        
        

    }
}

 

 

SubActivity

더보기
package com.jaybon.lifecycleex02;

import androidx.appcompat.app.AppCompatActivity;

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

public class SubActivity extends AppCompatActivity {

    private Button btnSubNum;


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

        btnSubNum = findViewById(R.id.btn_sub_num); // 버튼이름을 같게 해도 충돌안남
        btnSubNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}

 

 

Sub2Activity

더보기
package com.jaybon.lifecycleex02;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class Sub2Activity extends AppCompatActivity {

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

 

 

 

 

 

 

 

 

MainActivity

 

 

 

 

261 271

---------------

+ Recent posts