Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- intent
- 데이터 타입
- string
- DOMContentLoaded
- putextra
- 230508
- serializable
- Class
- C++
- DFS
- classList
- ActionBar
- 안드로이드
- 함수 인자
- Flutter
- parcelable
- 프래그먼트
- ViewPager
- html
- null-safety
- 230510
- 230503
- 부가데이터
- 인텐트
- textContent
- 생명주기
- javascript
- fragment
- Adapter
- querySelector
Archives
- Today
- Total
나만의 개발노트
[안드로이드] 인플레이션(inflation), 부분화면 본문
인플레이션(inflation)
: XML 레이아웃에 정의된 내용이 메모리에 객체화되는 과정
ex) xml레이아웃에 정의된 button을 안드로이드 시스템이 메모리에 만들어주는 것
- setContentView가 레이아웃 인플레이션 해줌
-> findViewById() 가 setContentView()보다 먼저 작성되면, 에러가 발생함
setContentView()
: xml과 java를 이어주는 함수
- 화면에 나타날 뷰를 지정하는 역할
- XML 레이아웃의 내용을 메모리 상에 객체화 하는 역할
setContentView(R.layout.레이아웃 파일 이름);
* 안드로이드는 한 쌍의 xml과 java가 Activity 하나를 이룸
(xml : 디자인, java : 실행방법)
- setContentView()는 전체화면을 지정
-> 부분화면만 사용할 때는, LayoutInflater 서비스를 사용해야함
LayoutInflatier
: 부분화면으로 새로운 xml을 넣을 때, inflation할 수 있게 해주는 것
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.sub1, container, true);
//sub1.xml은 부분화면에 사용할 xml
//container은 activity_main.xml에 sub.xml을 넣을 FrameLayout
//true는 바로 실행
-> activity_main.xml의 버튼을 누르면, sub1.xml이 부분화면으로 실행되는 앱 만들기
#MainActivity.java
package com.example.mylayoutinflater;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Inflater가 없어서 Sub1.xml에 있는 sub_Button은 객체화되지 않음
Button button1 = (Button) findViewById(R.id.sub_Button);
FrameLayout container = (FrameLayout) findViewById(R.id.container);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.sub1, container, true);
//sub1.xml의 inflater를 생성했기 때문에 sub_Button이 객체화 됨
Button subButton = (Button) findViewById(R.id.sub_Button);
}
});
}
}
#activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/container"
android:layout_width="409dp"
android:layout_height="665dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
#sub1.xml (res->layout->sub1.xml)
<?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"
android:background="#93E9EC">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="부분화면"
android:textSize="30dp"/>
<Button
android:id="@+id/sub_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
'[안드로이드] > [안드로이드] 공부 기록' 카테고리의 다른 글
[안드로이드] 원형이미지뷰(CircleImageView) (0) | 2023.10.07 |
---|---|
[안드로이드] 리스트뷰(ListView) , Adapter(어댑터) (1) | 2023.09.26 |
[안드로이드] inflate, toast, snapbar, AlertDialog (0) | 2023.09.25 |
[안드로이드] 이벤트 처리_터치 이벤트, 제스처 이벤트 (0) | 2023.09.18 |
[안드로이드] 안드로이드 기본 (0) | 2023.09.11 |