안드로이드에서 탭을 구성하는 예
3개의 탭을 각각 누를 때마다 동일한 액티비티가 실행되고 다른 이미지를 보여준다
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:configChanges="orientation" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".TabContentActivity" /> </application> </manifest>
res/layout/main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rootLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> </TabHost> </LinearLayout>
res/layout/tabcontents.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/ic_launcher" /> </LinearLayout>
MainActivity.java
package com.example.androidapp; import android.os.Bundle; import android.app.*; import android.content.Intent; import android.util.Log; import android.widget.*; public class MainActivity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.i("onCreate", "onCreate실행됨"); setTab(); } private void setTab() { TabHost tabhost = getTabHost(); TabHost.TabSpec spec = null; Intent intent = null; spec = tabhost.newTabSpec("tab01"); intent = new Intent(this, TabContentActivity.class); intent.putExtra("tabNum", "01"); spec.setContent(intent); spec.setIndicator("첫번째 사진"); tabhost.addTab(spec); spec = tabhost.newTabSpec("tab02"); intent = new Intent(this, TabContentActivity.class); intent.putExtra("tabNum", "02"); spec.setContent(intent); spec.setIndicator("두번째 사진"); tabhost.addTab(spec); spec = tabhost.newTabSpec("tab03"); intent = new Intent(this, TabContentActivity.class); intent.putExtra("tabNum", "03"); spec.setContent(intent); spec.setIndicator("세번째 사진"); tabhost.addTab(spec); tabhost.setCurrentTab(0); } }
TabContentActivity.java
package com.example.androidapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Parcelable; import android.util.Log; import android.widget.ImageView; import android.widget.TextView; public class TabContentActivity extends Activity { ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tabcontents); iv = (ImageView) findViewById(R.id.imageView1);
Intent intent = getIntent(); String tabNum = intent.getExtras().getString("tabNum"); if(tabNum.equals("01")) { iv.setImageResource(R.drawable.mt01); }else if(tabNum.equals("02")) { iv.setImageResource(R.drawable.mt02); }else if(tabNum.equals("03")) { iv.setImageResource(R.drawable.mt03); } Log.i("액티비티", "onCreate()실행됨,"+this); } }