在android中实现选项卡的效果可以通过两种方式:在布局文件中引用TabHost,在Activity中通过Id获取TabHost的实例,或者直接继承TabActivity,直接获取TabHost的实例。
程序主要代码如下:
1、布局文件tab.xml,在布局文件中需要注意的是:如果TabHost标签中引用TabWidget标签,则必须设置其android:id="@android:id/tabs",而FrameLayout标签的id必须设置为:android:id="@android:id/tabcontent",否则会出现空指针异常。
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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" //这
条语句的作用是把TabWidget设置到屏幕的下方,默认在屏幕上方。
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="match_parent"
android:layout_height="match_parent">
<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"/>
</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"/>
</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"/>
</LinearLayout>
</FrameLayout>
</RelativeLayout>
</TabHost>
Activity文件 TabDemo.java
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;
import com.kf.samples5.R;
public class TabDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
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(TabDemo.this, tabId, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 50);
toast.show();
}
});
// host.setCurrentTabByTag("Home");
Toast toast = Toast.makeText(TabDemo.this, "Home",
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 50);
toast.show();
}
}