Layout Inflation


Inflation


アプリケーションで画面を定義するために.xmlファイルを定義する必要があります.後で定義します.xmlファイルに関連付けられたアクティブクラスも定義する必要があります.
private val button : Button by lazy {
	findViewById(R.id.exampleButton)
}
通常、このようにレイアウトに関連付けられ、このときレイアウトの内容がメモリにオブジェクト化されるプロセスを인플레이션(Inflation)と呼ぶ.
レイアウトは、アプリケーションの実行時にメモリにオブジェクト化されます.つまり.xmlファイルにButtonが定義されていても、アプリケーションは自分が実行する前にボタンがあるかどうか分かりません.言い換えれば、setContentView(R.layout.activity_main)が呼び出されるまでボタンがあるかどうかはわかりません.
実際のコードで記述して実行します.
package com.example.event

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.GestureDetector
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.core.view.GestureDetectorCompat

class MainActivity : AppCompatActivity() {
    private val button : Button by lazy {
        findViewById(R.id.exampleButton)
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bind()
        setContentView(R.layout.activity_main)
    }

    private fun bind() {
        button.setOnClickListener {
            Toast.makeText(applicationContext, "버튼 눌림", Toast.LENGTH_LONG).show()
        }
    }
}
アプリケーションを実行すると死亡が表示されます.

どうしてこんなことになったの?これは、メモリにオブジェクト化されていないレイアウトを参照しようとしているためです.

実際には、エラーリストからNullPointExceptionが発生していることがわかります.
setContentView()が重要だと知っていますが、具体的にはどんな方法ですか?😐
setContentView()は、画面に表示するレイアウトまたはビューオブジェクトを指定します.上記のコードはまた、activity_mainをパラメータとして渡し、レイアウト内のビューをメモリにオブジェクト化することができる.では、画面全体ではなくsetContentView()としてレイアウトの一部または一部を表示できますか?残念ながらできません.setContentView()は、アクティブなフルスクリーン(メインレイアウト)の設定のみを担当するためです.
では、一部の画面はどのように客体化するのでしょうか...?😡
この場合、LayoutInflaterを使用することができる.LayoutInfolterを使用する方法は3つあります.(今朝からシャベルをよく使いました…)
  • getSystemService()
  • getLayoutInflater()
  • LayoutInflater.from()
  • getSystemService()


    これは最も基本的な方法であり、contextからLayoutInflaterまでである.

    getLayoutInflater()


    これはLayoutInflaterオブジェクトをインポートする方法です.自分のウィンドウのLayoutInflaterを使用します.

    LayoutInflater.from()


    最も一般的な方法は、LayoutInflaterにおいてstaticと定義されたLayoutInflater.fromによってオブジェクトを作成する方法である.同じcontextでは、同じオブジェクトが返されるため、メンバー変数として宣言する必要はありません.

    Infolateの表示


    インフレを使用するためにLayoutInfotionのinflaterを使用することができます.
    inflate(resource : Int, root : ViewGroup?, attachToRoot : Boolean)
  • リソース:ビューを作成するレイアウトファイルのid->R.id.~~.を渡します.
  • :作成するビューの親ビュー.つまり、作成するスペースのレイアウトオブジェクトを渡します.
  • attachTooroot:trueに設定すると、ルートのサブビューとして自動的に追加されます.
  • 実際のコードを見てみましょう.activity_mainは、FrameLayoutおよびButtonを有する.ボタンをクリックすると、FrameLayoutにTextViewを加える予定です.
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="300dp"
            android:layout_height="300dp">
        </FrameLayout>
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
    
    </LinearLayout>
    FrameLayoutに入れるTextViewです
    <?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"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="안녕하세요"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    main_activityでは、以上の3つの方法が使用されています.
    package com.example.example
    import android.content.Context
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.LayoutInflater
    import android.widget.Button
    import android.widget.FrameLayout
    
    class MainActivity : AppCompatActivity() {
        private val frame: FrameLayout by lazy {
            findViewById(R.id.frame)
        }
    
        private val button: Button by lazy {
            findViewById(R.id.button)
        }
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            bind()
        }
    
        private fun bind() {
            val inflater1: LayoutInflater =
                getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
            val inflater2: LayoutInflater = LayoutInflater.from(applicationContext)
            val inflater3: LayoutInflater = layoutInflater
            button.setOnClickListener {
                inflater1.inflate(R.layout.sub1, frame, true)
                inflater2.inflate(R.layout.sub1, frame, true)
                inflater3.inflate(R.layout.sub1, frame, true)
            }
        }
    }
    inflateの1番目のパラメータで表示されるレイアウトsub 1であり、2番目のパラメータは、sub 1をレイアウトに入れるオブジェクトである.実行時にボタンをクリックすると、追加テキストが表示されます.