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"
/>
'Front-end > Android (안드로이드 앱 개발)' 카테고리의 다른 글
Android studio (안드로이드 스튜디오) ListVIew, BaseAdapter, headerview, footerview / Spoon라디오 처럼 구현해보기 (0) | 2020.03.05 |
---|---|
Android studio (안드로이드 스튜디오) 전화번호부 만들기 2 (객체화), 3 (Glide) (0) | 2020.03.03 |
Android studio (안드로이드 스튜디오) AddView, inflater (0) | 2020.03.01 |
Android studio (안드로이드 스튜디오) Library 라이브러리 추가하기 ex) Glide 글라이드 (0) | 2020.02.29 |
Android studio (안드로이드 스튜디오) 플래그먼트 생성 2, create Fragment (0) | 2020.02.25 |
댓글