본문 바로가기
Front-end/Android (안드로이드 앱 개발)

Android studio (안드로이드 스튜디오) Layout에 View 동적 생성(추가)으로 전화번호부 만들기 1

by javapp 자바앱 2020. 3. 2.
728x90

전화번호부 만들어보기

 

            view = layoutInflater.inflate(R.layout.layout_complex, null, false);
            
//사진
            
ImageView imageView = view.findViewById(R.id.item_image);
            imageView.setImageResource(
imageList.get(i));
            
//이름
            
TextView nameText = view.findViewById(R.id.item_name);
            nameText.setText(
nameList.get(i));
            
//번호
            
TextView phoneText = view.findViewById(R.id.item_phonenum);
            phoneText.setText(
phoneList.get(i));
            
container.addView(view);

 

뷰를 동적으로 생성하고 addView를 통해 view를 추가하는 것이 핵심

 

 

< main.java >

public class MainActivity extends AppCompatActivity {
    ArrayList<String> nameList;ArrayList<String> phoneList;ArrayList<Integer> imageList;
    LayoutInflater layoutInflater;
    LinearLayout container;
    View view;
    Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;

        nameList = new ArrayList();phoneList = new ArrayList();imageList = new ArrayList();
        nameList.add("일하영");phoneList.add("010-0000-0000");imageList.add(R.drawable.ha1);
        nameList.add("이하영");phoneList.add("010-1111-1111");imageList.add(R.drawable.ha2);
        nameList.add("삼하영");phoneList.add("010-2222-2222");imageList.add(R.drawable.ha3);
        nameList.add("사하영");phoneList.add("010-3333-3333");imageList.add(R.drawable.ha4);
        nameList.add("송하영");phoneList.add("010-4444-4444");imageList.add(R.drawable.ha5);

        container = findViewById(R.id.container );
        layoutInflater = LayoutInflater.from(this);

        for(int i = 0; i < (nameList.size()); i++) {
            view = layoutInflater.inflate(R.layout.layout_complex, null, false);
            //사진
            ImageView imageView = view.findViewById(R.id.item_image);
            imageView.setImageResource(imageList.get(i));
            //이름
            TextView nameText = view.findViewById(R.id.item_name);
            nameText.setText(nameList.get(i));
            //번호
            TextView phoneText = view.findViewById(R.id.item_phonenum);
            phoneText.setText(phoneList.get(i));
            container.addView(view);
        }
    }
}

 

< main.xml >

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

<TextView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginBottom="10dp"
    android:text="전화번호부"
    android:textSize="50dp"
    android:textStyle="bold"
    android:gravity="center"
    />
</LinearLayout>

 

 

< layout_complex.xml >

 

<ImageView
    android:id="@+id/item_image"
    android:layout_width="110dp"
    android:layout_height="100dp"
    />

<TextView
    android:id="@+id/item_name"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:text="홍길동"
    android:gravity="center"
    android:textSize="20dp"
    />

<TextView
    android:id="@+id/item_phonenum"
    android:layout_width="200dp"
    android:layout_height="100dp"
    android:text="010-1234-5678"
    android:gravity="center"
    android:textSize="20dp"
    android:layout_marginBottom="10dp"
    />

 

 

댓글