2010/04/23

[Android Dev.] Tab 사용하기 - TabWidget 에 View 올리기

TabWidget 의 탭 타이틀을 설정하는 setIndicator 함수는 View 을 매개변수로 받는 함수도 존재 한다. 따라서 View 를 구성하여 Tab의 타이틀에 올릴 수 있다.



TabWidget 에 올려질 View 를 구성한 XML 리소스

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/IVIcon"
android:src="@drawable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/TVCaption"
android:text="Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>




TabWidget 에 View 를 올리는 소스

TabHost tabHost = (TabHost)findViewById( R.id.tabHost );

// TabHost 를 findViewById 로 생성한 후 Tab 추가 전에 꼭 실행해 주어야 함.
tabHost.setup();

// 새로운 Tab 을 생성하기 위한 Tab 객체 생성
TabHost.TabSpec spec;

// Custom View for TabWidget
LayoutInflater layout = getLayoutInflater();

// 첫 번째 Tab 설정 및 등록
spec = tabHost.newTabSpec( "Tab 00" ); // 새 Tab 생성
View vTab1 = layout.inflate( R.layout.mytabwidget, null );
TextView tvCaption = (TextView)vTab1.findViewById( R.id.TVCaption );
tvCaption.setText( "Custom Tab 1" );
spec.setIndicator( vTab1 ); // Tab 제목
spec.setContent(R.id.layout); // Tab 내용
tabHost.addTab( spec ); // 생성 된 Tab 등록

// 두 번째 Tab 설정 및 등록
spec = tabHost.newTabSpec( "Tab 01" ); // 새 Tab 생성
View vTab2 = layout.inflate( R.layout.mytabwidget, null );
tvCaption = (TextView)vTab2.findViewById( R.id.TVCaption );
tvCaption.setText( "Custom Tab 2" );
spec.setIndicator( vTab2 ); // Tab 제목
spec.setContent( R.id.btnClickme ); // Tab 내용
tabHost.addTab( spec ); // 생성 된 Tab 등록

// 첫 번째 탭을 활성화
tabHost.setCurrentTab( 0 );




Custom View 를 올릴 경우 선택된 탭과 선택되지 않는 탭의 구분이 모호해 지는 경우가 있는데 이 부분에 대한 처리는 좀더 공부를 해 봐야 겠다.<

Original Post : http://neodreamer-dev.tistory.com/421

No comments :

Post a Comment