반응형
Greensky0026
삽질러의 대환장 공사판
Greensky0026
전체 방문자
오늘
어제
  • 분류 전체보기 (241)
    • Language (56)
      • JAVA (13)
      • Swift (38)
      • Kotlin (4)
      • Dart (0)
      • PHP (0)
      • JavaScript (1)
    • IDE & Framework (92)
      • Spring (10)
      • Android (48)
      • iOS (8)
      • NodeJS (1)
      • CodeIgniter (3)
      • Flutter (1)
      • 분류중 (18)
    • Infra (8)
    • Database (12)
    • VCS (4)
    • Debug Log (34)
      • java (7)
      • swift (3)
      • Android (9)
      • Web (7)
      • 삽질기 (7)
    • Others (19)
      • 코딩테스트 풀이 (0)
      • IT 이야기 (18)
    • 쉼터 (2)
    • 개발공부 (14)
      • Network (1)
      • 자료구조와 알고리즘 (2)
      • Code design (8)
      • ETC (1)
      • 아카이브 (1)

블로그 메뉴

    공지사항

    • 프로그래머스 코딩 테스트 관련글 비공개 처리

    인기 글

    태그

    • ios
    • 코딩테스트
    • 타입
    • IT
    • map
    • 구축
    • 프로그래밍
    • 개발
    • JSP
    • Java
    • swift
    • swfit
    • kotlin
    • Android
    • 프로그래머스
    • xcode
    • 공부
    • 예제
    • reduce
    • level1

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    Greensky0026

    삽질러의 대환장 공사판

    LayoutInflater는 무엇일까?
    IDE & Framework/Android

    LayoutInflater는 무엇일까?

    2021. 6. 5. 17:44
    반응형

    Inflater는 부풀리다 라는 사전적 의미를 가지고 있습니다.

    그러면 LayoutInflater는 레이아웃을 부풀린다는 뜻인데, 왜 부풀리다고 하는 것일까요? 알아봅시다!

     

     우선, 클래스에서 레이아웃을 어떻게 매칭시키는지부터 알아야 합니다.

    • Activity를 생성하게 되면 클래스파일 하나와 레이아웃파일 하나가 같이 생성
    • onCreate메소드가 자동으로 생성
    • setContentView(R.layout.activity_order_dialog) 이 바로 클래스와 레이아웃을 메모리에 로드

    이로써 findViewById와 같은 메소드로 레이아웃 리소스드들을 id를 매개변수로 주어 핸들링 할 수 있는 것이죠.

     

    그런데, 커스텀 리스트뷰나 커스텀 다이얼로그에 보여줄 레이아웃을 만드 경우에는

    XML resource파일만 생성하게 됩니다.

    이대로라면 그냥 XML 형식으로 된 파일일 뿐입니다.

    여기서 LayoutInflater를 사용해 메모리에 적재해 app에서 사용할 준비를 해주는 것이죠.

     

    inflater의 사용방법

    1. xml 파일 제작

    2. LayoutInflater의 인스턴스 생성

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    3. 메모리에 적재

        inflater.inflater( 메모리에 로드할 xml 파일, xml를 사용할 부모 레이아웃, 바로 인플레이션할지 여부)

       

    그리고 자매품으로 Inflater.Inflate도 한번 살펴볼까요?

        /**
         * Inflate a new view hierarchy from the specified xml resource. Throws
         * {@link InflateException} if there is an error.
         *
         * @param resource ID for an XML layout resource to load (e.g.,
         *        <code>R.layout.main_page</code>)
         * @param root Optional view to be the parent of the generated hierarchy.
         * @return The root View of the inflated hierarchy. If root was supplied,
         *         this is the root View; otherwise it is the root of the inflated
         *         XML file.
         */
        public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
            return inflate(resource, root, root != null);
        }

     

    inflate를 사용하는 세가지 과정

    1. 인플레이터를 인스턴스를 생성

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    2. xml의 root view타입을 선언
        LinerarLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.inflate_example , null);

    3. 메모리에 로드
        setContextView(linearLayout);

     

     

     

    하지만, View 에도 inflate메소드가 존재하는걸 아시나요?

        /**
         * Inflate a view from an XML resource.  This convenience method wraps the {@link
         * LayoutInflater} class, which provides a full range of options for view inflation.
         *
         * @param context The Context object for your activity or application.
         * @param resource The resource ID to inflate
         * @param root A view group that will be the parent.  Used to properly inflate the
         * layout_* parameters.
         * @see LayoutInflater
         */
        public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
            LayoutInflater factory = LayoutInflater.from(context);
            return factory.inflate(resource, root);
        }

    이는 

    LinearLayout lin = (LinearLayout)View.inflate(getApplicationContext(), R.layout.order_dialog_layout, null);

     이렇게 바로 사용할 수 있습니다.

    커스텀 다이얼로그를 Builder로 사용할때 사용한 방법인데요,

    이후 .setView(lin)를 사용해 다이얼로그의 view로 사용하게 됩니다.

    반응형
    저작자표시 (새창열림)

    'IDE & Framework > Android' 카테고리의 다른 글

    Recycler View item handeling  (0) 2021.06.21
    Custom ListView 만들기(1/2) - 설계  (0) 2021.06.05
    비동기(Async)통신과 동기(Sync)통신의 차이 feat. retrofit  (0) 2021.05.08
    margin과 padding 차이  (0) 2021.05.08
    Android Activity 수명주기란?  (0) 2021.05.07
      'IDE & Framework/Android' 카테고리의 다른 글
      • Recycler View item handeling
      • Custom ListView 만들기(1/2) - 설계
      • 비동기(Async)통신과 동기(Sync)통신의 차이 feat. retrofit
      • margin과 padding 차이
      Greensky0026
      Greensky0026
      점이 모여 선이 된다. 내 삽질도 언젠간 거대한 지하 도시가 되겠지!

      티스토리툴바