For developer/Android

(my09_layoutinflate)인플레이트

프린이0218 2020. 5. 21. 18:11

Inflate이란  xml 에 표기된 레이아웃들을 메모리에 객체화시키는 행동이다.

쉽게 말해서, XML 코드에 자바랑 연동해서 조작을 하기 위해서 사용한다.

 

 

 

 

 

더보기
package com.example.my09_layoutinflate;

        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.LinearLayout;
        import android.widget.RelativeLayout;
        import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button btnMain ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnMain = findViewById(R.id.btnMain);
        btnMain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout linear = findViewById(R.id.linear);
                RelativeLayout relative = findViewById(R.id.relative);

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

                inflater.inflate(R.layout.sub1_layout,linear,true);
                inflater.inflate(R.layout.sub2_layout,relative,true);
                
                Button btnSub1 = linear.findViewById(R.id.button1);
                Button btnSub2 = relative.findViewById(R.id.button2);
                
                btnSub1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "리니어 버튼 클릭 !!!", Toast.LENGTH_SHORT).show();
                    }
                });

                btnSub2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this, "랠리티브 버튼 클릭 !!!", Toast.LENGTH_SHORT).show();
                    }
                });

            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="sub1버튼" />

    <CalendarView
        android:id="@+id/calendarView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="1dp"
        android:text="sub2 버튼" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginStart="154dp"
        android:layout_marginLeft="154dp"
        android:layout_marginTop="77dp"
        android:layout_marginEnd="148dp"
        android:layout_marginRight="148dp"
        app:srcCompat="@drawable/ic_launcher_foreground" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginStart="99dp"
        android:layout_marginLeft="99dp"
        android:layout_marginTop="62dp"
        android:layout_marginEnd="72dp"
        android:layout_marginRight="72dp" />

    <SeekBar
        android:id="@+id/seekBar"
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="190dp"
        android:layout_height="91dp"
        android:layout_below="@+id/ratingBar"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginTop="51dp"
        android:layout_marginEnd="98dp"
        android:layout_marginRight="98dp"
        android:max="10"
        android:progress="3" />
</RelativeLayout>