2010/04/26

[Android Dev.] Tab 사용하기 - 동적으로 Tab 추가하기

아래 코드는 XML 리소스에서 탭을 구성하는 것이 아니고 필요에 따라 코드로 탭을 동적으로 추가하는 내용이다.

동적으로 Tab 을 추가 하기위해서는 TabHost.TabContentFactory() 이용해 Content를 생성한다.






main.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">
<TabHost
android:id="@+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="69px"
>
<LinearLayout
android:id="@+id/BtnLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="@+id/BtnLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Linear Layout"/>
<Button
android:id="@+id/BtnRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Relative Layout"/>
<Button
android:id="@+id/BtnTableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Table Layout"/>
</LinearLayout>
</FrameLayout>
</TabHost>
</LinearLayout>


TabHost 생성 및 Tab 추가 코드

setContentView( R.layout.main_dynamic_tab );

// Tab Host 객체 생성 - 다른 객체의 멤버 함수에서 사용하기 위해 final 키워드 이용
final TabHost tabHost = (TabHost)findViewById( R.id.tabHost );

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

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

// 첫 번째 Tab 설정 및 등록
spec = tabHost.newTabSpec( "Tab 00" ); // 새 Tab 생성
spec.setIndicator("Layouts", getResources().getDrawable(R.drawable.icon) ); // Tab 제목, 아이콘
spec.setContent(R.id.BtnLayout); // Tab 내용
tabHost.addTab( spec ); // 생성 된 Tab 등록

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

// Linear Layout
Button btnAddLinearLayout = (Button)findViewById( R.id.BtnLinearLayout );
btnAddLinearLayout.setOnClickListener( new OnClickListener()
{
@Override
public void onClick(View v)
{
for ( int view = 0; view < tabHost.getChildCount(); ++view )
{

}

TabHost.TabSpec spec = tabHost.newTabSpec( "Linear Layout" );

spec.setIndicator( "Linear" );
spec.setContent( new TabHost.TabContentFactory()
{
@Override
public View createTabContent(String tag)
{
LinearLayout linear = new LinearLayout( MyLayout.this );
linear.setOrientation( LinearLayout.VERTICAL );
linear.setId( MY_LINEARLAYOUT_VIEW );

LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT );

linear.setLayoutParams( param );

// EditText
EditText etName = new EditText( MyLayout.this );
etName.setText( "Input your name here!!" );
linear.addView( etName );

// First Button
Button btn = new Button( MyLayout.this );
btn.setText( "First Added Button" );
linear.addView( btn, param );

// Second Button
btn = new Button( MyLayout.this );
btn.setText( "Second Added Button" );
linear.addView( btn, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT ) );

// TextView
TextView tvHello = new TextView( MyLayout.this );
tvHello.setText( "Hello! Linear layout!!" );
tvHello.setGravity( Gravity.CENTER );
linear.addView( tvHello );

// Third Button
btn = new Button( MyLayout.this );
btn.setText( "Third Added Button" );
linear.addView( btn, param );

return linear;
}
});

tabHost.addTab( spec );
}
});

<

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

No comments :

Post a Comment