부분 레이아웃을 전체 레이아웃에 메모리에 객체화 시키기.

2022. 1. 11. 15:20·안드로이드 with 자바/자잘한 문제들

1. 구현해야 할 것

( 버튼을 클릭하면 아래의 그림처럼 부분 레이아웃이 나오게 된다. )

 

 

 

 

2. MainActivity.java

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 MainActivity extends AppCompatActivity {
    LinearLayout container;

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

        container = findViewById(R.id.container);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Button btn1 = findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                inflater.inflate(R.layout.sub, container, true);
            }
        });
    }
}

 

 

 

 

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

    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="버튼을 누르세요." />

    <Button
        android:id="@+id/btn1"
        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>

 

 

 

 

4. sub.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/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="부분 화면"
        android:textSize="30sp" />

    <CheckBox
        android:id="@+id/cb1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CheckBox" />
</LinearLayout>

'안드로이드 with 자바 > 자잘한 문제들' 카테고리의 다른 글

BitmapDrawable을 통한 스크롤 뷰 구현  (0) 2022.05.11
Toast 직접 만들어 표시하기.  (0) 2022.01.06
[3] 두 개의 이미지 주고 받기.  (0) 2021.12.28
[4] SMS 입력 화면 만들고 글자의 수 표시하기  (0) 2021.12.28
java 코드에서 화면 구성  (0) 2021.12.22
'안드로이드 with 자바/자잘한 문제들' 카테고리의 다른 글
  • BitmapDrawable을 통한 스크롤 뷰 구현
  • Toast 직접 만들어 표시하기.
  • [3] 두 개의 이미지 주고 받기.
  • [4] SMS 입력 화면 만들고 글자의 수 표시하기
백_곰
백_곰
  • 백_곰
    친절한 코딩
    백_곰
  • 전체
    오늘
    어제
    • 분류 전체보기
      • 알고리즘 (with JAVA)
        • 기본 알고리즘
        • 완전 탐색
        • 분할 정복 알고리즘
        • 동적 계획법
        • 탐욕법
        • 코딩 테스트 기출 문제
        • 코드트리 조별과제
      • 백준 (with JAVA)
        • 완전 탐색
        • 분할 정복
        • 그 외
      • 자바
        • 개발 환경 구축하기
        • 팁
        • 기본적인 개념
        • 컬렉션 프레임워크
        • 프로세스와 쓰레드
        • 지네릭스
        • 람다식
        • 스트림
        • 입출력 IO
        • 네트워킹
        • 열거형(enums)
        • java.lang 패키지
        • java.time 패키지
        • 유용한 클래스들
        • 형식화 클래스들
      • 안드로이드 with 자바
        • 응용 문제들
        • 자잘한 문제들
        • 오류 보고서
  • 블로그 메뉴

    • 링크

    • 공지사항

    • 인기 글

    • 태그

      serializable
      안정 정렬
      역직렬화
      다형성
      java.lang패키지
      선택 정렬
      유용한 클래스
      InputStream
      스트림
      코딩테스트
      코드트리
      알고스팟
      file
      코딩트리조별과제
      불안정 정렬
      Arrays
      map()
      소켓 프로그래밍
      문자 기반 스트림
      TCP 소켓 프로그래밍
      ServerSocket
      람다식
      중간연산
      java.time 패키지
      snail
      Collections Framework
      제자리 정렬
      outputstream
      자바 개념
      안드로이드 스튜디오
    • 최근 댓글

    • 최근 글

    • hELLO· Designed By정상우.v4.10.3
    백_곰
    부분 레이아웃을 전체 레이아웃에 메모리에 객체화 시키기.
    상단으로

    티스토리툴바