xml的部分
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- set the tab title attributes and set the title align the bottom -->
<TabWidget
android:id="@android:id/tabs"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
<!-- set the tab body attributes -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/tab1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="20dip">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/home_normal"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="20dip">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/garbage_normal"/>
</LinearLayout>
<LinearLayout
android:id="@+id/tab3"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="20dip">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/help_normal"/>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
主程式
package test.c;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;
public class test extends Activity {
/** Called when the activity is first created. */
public static TabHost tab_host;
private TabHost mTabHost = null;
//private TabWidget mTabWidget = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//tag開始
TabHost host = (TabHost)findViewById(R.id.tabhost);
host.setup();
TabHost.TabSpec homeSpec = host.newTabSpec("Home"); //This param will be used as tabId.
homeSpec.setIndicator(null, //This param will diplay as title.
getResources().getDrawable(R.drawable.home_normal));
homeSpec.setContent(R.id.tab1);
host.addTab(homeSpec);
TabHost.TabSpec garbageSpec = host.newTabSpec("Garbage");
garbageSpec.setIndicator(null, getResources().getDrawable(R.drawable.garbage_normal));
garbageSpec.setContent(R.id.tab2);
host.addTab(garbageSpec);
TabHost.TabSpec maybeSpec = host.newTabSpec("Help");
maybeSpec.setIndicator(null, getResources().getDrawable(R.drawable.help_normal));
maybeSpec.setContent(R.id.tab3);
host.addTab(maybeSpec);
host.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(test.this, tabId, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 50);
toast.show();
}
});
// host.setCurrentTabByTag("Home");
Toast toast = Toast.makeText(test.this, "Home", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 50);
toast.show();
}
}
//tag finish
小提醒 記得放入圖片到 layout ==>drawable
沒有留言:
張貼留言