Android

안드로이드 동적 뷰생성 ( inflate )

Machine_웅 2021. 9. 2. 14:07
728x90
반응형

준비물 

1. 만들 뷰 xml 생성

2. 저장하고싶은 LinearLayout 생성 

3. 동적으로 생성된 뷰 클래스 만들기 ( 선택사항 ) 

 

 

 

1.  만들 뷰 xml 생성 

<?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"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:gravity="center_vertical">

            <TextView
                android:id="@+id/txt_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="4dp"
                android:text="매매금액"
                android:textColor="#000"
                android:textSize="@dimen/menu_subTitle" />

            <ImageView
                android:id="@+id/image_red"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:background="#F00" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/edit_money"
                android:layout_width="0dp"
                android:layout_height="@dimen/menu_edit_h"
                android:layout_weight="3"
                android:background="@drawable/edit_box_white"
                android:paddingLeft="10dp"
                android:textSize="15dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingLeft="10dp"
                android:textSize="18dp"
                android:text="원"
                android:textColor="#000" />

        </LinearLayout>

        <LinearLayout
            android:layout_marginTop="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/txt_money"
                android:text="이천만"
                android:gravity="right"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:paddingLeft="10dp"
                android:textSize="15dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingLeft="10dp"
                android:text="원"
                android:textColor="#000" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

 

2. 저장 하고 싶은 Layout 

            <LinearLayout
                android:id="@+id/view_add_layout"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

3. 뷰클래스  ( 선택사항 ) 

public class Layout_money extends LinearLayout {

    public Layout_money(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        init(context);

    }

    public Layout_money(Context context) {
        super(context);

        init(context);
    }

    private void init(Context context){
        LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_money,this,true);
    }
}

 

4 - 1. 뷰 인플레이트 

    btn_sType01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View itemView = View.inflate(getApplicationContext(),R.layout.view_money,null);
                view_add_layout.addView(itemView);
            }
        });

 

4-2. 클래스를 활용한 인플레이트 

    btn_sType01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               /* View itemView = View.inflate(getApplicationContext(),R.layout.view_money,null);
                view_add_layout.addView(itemView);*/

                Layout_money subView = new Layout_money(getApplicationContext());
                view_add_layout.addView(subView);
            }
        });

 

사용예시 )

public class Layout_money extends LinearLayout {

    View view;
    TextView txt_title;
    ImageView image_red;
    EditText edit_money;
    TextView txt_money;


    public Layout_money(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public Layout_money(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context){
        LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.view_money,this,true);

        txt_title = view.findViewById(R.id.txt_title);
        image_red = view.findViewById(R.id.image_red);
        edit_money = view.findViewById(R.id.image_red);
        txt_money = view.findViewById(R.id.txt_money);

    }
}

 

 

참고 및 출처 

https://coding-factory.tistory.com/44

 

728x90
반응형