0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AndroidStudio: LayoutInflatter の使い方

Last updated at Posted at 2023-11-08

こちらと同じことを Kotlin で行いました。ViewBinding も使いました。
[Android] inflate を使ってみる

プロジェクトの作成

プロジェクト名: layoutflatter01

環境

ツリー構造
image.png

ViewBinding を使う設定

app/build.gradle.kts
(省略)
android {
(省略)
    buildFeatures {
    viewBinding=true
  }
}
(省略)

res/drawable/img.png は適当な画像を貼り付けます。
例えば、
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
            }
        }
    }
}

実行結果

起動直後
image.png

ボタンクリック後
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?