▶ Layout Inflater 예제.
// main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="여기에 사용자 이름을 입력하세요"
/>
<LinearLayout
android:id="@+id/contentsLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
</LinearLayout>
// button.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/btnSelect"
android:text=" 선택 "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<RadioGroup
android:id="@+id/radioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/radio01"
android:text="남성"
/>
<RadioButton
android:id="@+id/radio02"
android:text="여성"
/>
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="하루종일"
/>
<CheckBox
android:id="@+id/allDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
▶ SampleLayoutInflator. java
package com.example.samplelayoutinflator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
public class SampleLayoutInflator extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 컨텐츠를 따로 추가할 레이아웃 객체 참조
LinearLayout contentsLayout = (LinearLayout) findViewById(R.id.contentsLayout);
// Layout Inflater 객체 참조
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// button.xml에 정의된 레이아웃에 대한 인플레이션 수행
inflater.inflate(R.layout.button, contentsLayout, true);
// 버튼과 체크 박스 객체 참조
Button btnSelect = (Button) findViewById(R.id.btnSelect);
final CheckBox allDay = (CheckBox) findViewById(R.id.allDay);
// button event 처리
btnSelect.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (allDay.isChecked()) {
allDay.setChecked(false);
} else {
allDay.setChecked(true);
}
}
});
}
}
▶ 결과 화면
[Android] Activity 와 Stack Flag (0) | 2012.08.11 |
---|---|
[Android]Intent 예제 (0) | 2012.08.11 |
[Android]RelativeLayout 예제 (0) | 2012.08.08 |
[Android] 안드로이드 최근 버전의 주요 특징 (0) | 2012.08.04 |
[Android]howto_SDK_git_cygwin (0) | 2012.06.21 |
댓글 영역