こちらと同じことを Kotlin で行いました。ViewBinding も使いました。
[Android] inflate を使ってみる
プロジェクトの作成
プロジェクト名: layoutflatter01
環境
ViewBinding を使う設定
app/build.gradle.kts
(省略)
android {
(省略)
buildFeatures {
viewBinding=true
}
}
(省略)
res/drawable/img.png は適当な画像を貼り付けます。
例えば、
res/values/strings.xml
<resources>
<string name="app_name">layoutflatter01</string>
<string name="button">Button</string>
<string name="description">Yuka</string>
<string name="text">Birthday Events!</string>
<string name="contents">
"日付:\n"
"20XX年05月22日\n"
"会場:\n"
"東京都新宿区山吹町 Bスタジオ\n\n"
"第1部:10時00分~11時00分\n"
"(手作りケーキ?お菓子を食べよう!)\n"
"第2部:11時15分~12時15分\n"
"(ゆかちぃとゲーム!<頭脳編>)\n"
"握手会\n"
"12時30分~13時00分\n"
"(握手会:その1)\n"
"第3部:"
"14時00分~15時00分\n"
"(ゆかちぃとゲーム!<アクティブ編>)\n"
"第4部:"
"15時15分~16時15分"</string>
</resources>
画面
activity_main.xml
<?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:padding="16dp"
android:orientation="vertical"
android:background="#000"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="20dp"
android:text="@string/button" />
<LinearLayout
android:id="@+id/insert_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<TextView
android:id="@+id/textView"
android:textColor="#ff0"
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
inflate_layout.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:text="@string/text"
android:gravity="center"
android:textColor="#ff0"
android:textSize="30sp"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:src="@drawable/img"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/description"/>
</LinearLayout>
プログラム
MainActivity.kt
package com.example.layoutflatter01
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.LinearLayout
import com.example.layoutflatter01.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var linearLayout: LinearLayout
private var flag = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
linearLayout = findViewById(R.id.insert_layout)
binding.textView.text = getString(R.string.contents)
binding.button.setOnClickListener {
if (!flag) {
layoutInflater.inflate(R.layout.inflate_layout,linearLayout)
flag = true
} else {
linearLayout.removeAllViews()
flag = false
}
}
}
}