반응형
app 디렉토리에서 우클릭 - new - Activity - Empty Activity
이름을 MenuActivity로 함. 레이아웃 네임에 activity_menu로 되어있는데, res/layout에 activity_menu.xml이 자동으로 생성되어 있음
activity_menu.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=".MenuActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
id를 container로 해놓은 리니어 레이아웃은 부분 화면이 들어갈 공간을 확보하기 위해 넣은 것
res/layout에 sub1.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">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="부분 화면 1" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>
MenuActivity
package com.example.a19_samplelayoutinflater;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MenuActivity extends AppCompatActivity {
TextView textView;
Button button;
LinearLayout container;
CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
container = findViewById(R.id.container);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// getSystemService - https://promobile.tistory.com/169
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// sub1을 container 안에 넣음
inflater.inflate(R.layout.sub1, container, true);
checkBox = container.findViewById(R.id.checkBox);
checkBox.setText("로딩됨");
}
});
}
}
app/manifests/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a19_samplelayoutinflater">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.A19_SampleLayoutInflater">
<activity android:name=".MenuActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>
</application>
</manifest>
반응형
'안드로이드' 카테고리의 다른 글
인텐트 (0) | 2021.10.14 |
---|---|
여러 화면 전환 (0) | 2021.10.13 |
시크바와 프로그레스바 보여주기 (0) | 2021.10.12 |
두 종류의 버튼 모양 만들기 (0) | 2021.10.12 |
프로그레스바 (0) | 2021.10.12 |