* Layout에 다른 Layout넣기 *
1. 화면 전체에 나타낼 Layout을 메모리 상에 객체화할 때는 setContentView()를 사용한다.
2. 화면 일부만 별로 Layout을 메모리 상에 객체화 하려면 별도 인플레이션 객체를 사용해야 된다.
안드로이드에서는 LayoutInflater 클래스를 이용한다.
이 클래스는 시스템 서비스로 제공되므로 getsystemService(Context.LAYOUT_INFLATER_SERVICE) 메소드를 사용하여
객체를 참조한 후 사용해야 한다.
3. 아래 소스중 activity_main.xml 에서 보면 ["@+id/contentsLayout"] id인 LinearLayout에 위치에 별도 Layout을 넣게 된다.
[ activity_main.xml ]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="40dp" android:gravity="center_horizontal" android:text="아래 버튼을 누르면 인플레이션으로 추가합니다." android:textSize="14dp" android:textColor="#ffad6535" />
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="인플레이션으로 추가하기" android:textSize="22dp" android:onClick="onButton1Clicked" />
<LinearLayout android:id="@+id/contentsLayout" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > </LinearLayout>
</LinearLayout> |
[ button.xml ]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<Button android:id="@+id/selectButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="선택 " android:textSize="24dp" android:textStyle="bold" /> <RadioGroup android:id="@+id/radioGroup01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingLeft="5dp" android:paddingRight="5dp" > <RadioButton android:id="@+id/radio01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="남성" android:textColor="#ffaaff10" android:textStyle="bold" android:textSize="24dp" /> <RadioButton android:id="@+id/radio02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="여성" android:textColor="#ffaaff10" android:textStyle="bold" android:textSize="24dp" /> </RadioGroup> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical|center_horizontal" android:paddingTop="10dp" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="하루종일" android:textSize="24dp" android:paddingRight="10dp" android:textColor="#ffaaff10" /> <CheckBox android:id="@+id/allDay" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout> |
[ MainActivity.java ]
/** * 레이아웃 인플레이터를 이용해 레이아웃의 일부를 동적으로 로딩하는 방법에 대해 알 수 있습니다. * * @author Mike */ public class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
/** * 버튼을 눌렀을 때 레이아웃을 동적으로 로딩합니다. * @param v */ public void onButton1Clicked(View v) { inflateLayout(); }
/** * button.xml 에 정의된 레이아웃을 메인 액티비티의 레이아웃 일부로 추가하는 메소드 정의 */ private void inflateLayout() { // XML 레이아웃에 정의된 contentsLayout 객체 참조 LinearLayout contentsLayout = (LinearLayout) findViewById(R.id.contentsLayout);
// 인플레이션 수행 LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); // inflate()메소드의 파라미터로 R.layout.button과 contentsLayout객체를 전달하고 앋.
//이것은 contentsLayout을 부모 컨테이너로 하여 buttons.xml파일에 정의된 레이아우승ㄹ 추가하라는 의미이다.
inflater.inflate(R.layout.button, contentsLayout, true);
// 새로 추가한 레이아웃 안에 들어있는 버튼 객체 참조 Button btnSelect = (Button) findViewById(R.id.selectButton); final CheckBox allDay = (CheckBox) findViewById(R.id.allDay);
btnSelect.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (allDay.isChecked()) { allDay.setChecked(false); } else { allDay.setChecked(true); } } }); } } |