# App/Android(JAVA)

[안드로이드 스튜디오] View를 동적으로 생성하기 (자바에서 생성)

1
2
3
4
5
6
7
8
9
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical">
        <HorizontalScrollView
            android:id="@+id/layoutSeat"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </ScrollView>
cs








1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
int seatGaping = 10;
 
layout = findViewById(R.id.layoutSeat);
 
 
LinearLayout layoutSeat = new LinearLayout(this);
layoutSeat.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutSeat.setLayoutParams(params);
layoutSeat.setPadding(8 * seatGaping, 8 * seatGaping, 8 * seatGaping, 8 * seatGaping);
layout.addView(layoutSeat);
 
LinearLayout layout = new LinearLayout(this); // 
layout.setOrientation(LinearLayout.HORIZONTAL);
layoutSeat.addView(layout);
 
TextView view = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(seatSize, seatSize);
layoutParams.setMargins(seatGaping, seatGaping, seatGaping, seatGaping);
view.setLayoutParams(layoutParams);
view.setPadding(0002 * seatGaping);                
view.setId(count);             
view.setGravity(Gravity.CENTER);
view.setBackgroundResource(R.drawable.ic_seats_booked);
view.setTextColor(Color.WHITE); //텍스트컬러
view.setTag(pos); //태그
view.setText(count + "");
view.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 9); // textSize
layout.addView(view);
seatViewList.add(view);
view.setOnClickListener(this);
cs


728x90