-
[ANDROID] 동적 layout 추가 삭제(LayoutInflater)ANDROID 2020. 1. 7. 17:26반응형
LayoutInflater을 이용하여 동적 layout 추가, 삭제를 한다.
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="버튼을 눌러 부분 화면을 추가하세요." android:textSize="20sp" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="추가하기" /> <Button android:id="@+id/btn_delete" 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>
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"> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="부분 화면 1" android:textSize="30sp" /> <CheckBox android:id="@+id/checkBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="동의합니다." /> </LinearLayout>
MenuActivity.java
package com.example.samplelayoutinflater; 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.CheckBox; import android.widget.LinearLayout; public class MenuActivity extends AppCompatActivity { LinearLayout container; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_menu); container = findViewById(R.id.container); Button button = findViewById(R.id.button2); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); /* LayoutInflater inflater = LayoutInflater.from(MenuActivity.this); LayoutInflate의 form 메서드를 호출 해도 가능 */ inflater.inflate(R.layout.sub1, container, true); CheckBox checkBox = container.findViewById(R.id.checkBox); checkBox.setText("로딩되었습니다."); } }); Button btnDelete = (Button) findViewById(R.id.btn_delete); btnDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { container.removeAllViews(); } }); } }
반응형'ANDROID' 카테고리의 다른 글
[ANDROID] RETROFIT2 파일 + 텍스트 전송 (2) 2020.01.17 [ANDROID] 이전 Activity 로 데이터 전송하기 (0) 2020.01.08 [ANDROID] MainActivity.java 에서 다른 xml ID 호출(LayoutInflater) (0) 2020.01.07