안드로이드 레이아웃 동적 구성.
xml 레이아웃을 이용하여 view 등록
private void setupUI() {
// 임시 데이터 생성.
ArrayList<String> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add("i data ==" + i);
}
// 데이터 만큼 뷰 등록.
for (int i = 0; i < list.size(); i++) {
View view = LayoutInflater.from(this).inflate(R.layout.layout_sub, main_Layout, false);
main_Layout.addView(view);
TextView first = view.findViewById(R.id.first_textview);
TextView second = view.findViewById(R.id.second_textview);
TextView third = view.findViewById(R.id.third_textview);
first.setText("list :" + list.get(i));
view.setTag("" + list.get(i));
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("bear", "tag : " + v.getTag());
}
});
}
}
////////////////////////////////////////////////////////////////////////////////////////
layout_sub.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:layout_gravity="center"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#ccc"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="1 :" />
<TextView
android:id="@+id/first_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="2 : " />
<TextView
android:id="@+id/second_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="3 : " />
<TextView
android:id="@+id/third_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="3" />
</LinearLayout>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
</android.support.constraint.ConstraintLayout>
'Android > source code' 카테고리의 다른 글
안드로이드 추상 클래스 이용한 퍼미션 권한 설정. 2-2 (0) | 2018.10.15 |
---|---|
Android RecyclerView 예제 (0) | 2018.10.15 |
액티비티 (0) | 2018.09.18 |
안드로이드 추상 클래스 이용한 퍼미션 권한 설정. 2-1 (0) | 2018.08.20 |
MVP 패턴 코틀린으로 작업 (0) | 2018.08.14 |