在Android UI开发时,使用Tab选项卡可以实现多个不同标签内容的切换,要实现这个效果,首先要得到TabHost对象,它是用来存放多个Tab标签的容器,每个Tab都可以对其显示的内容进行自定义布局,在得到TabHost后,通过addTab()方法来向TabHost中添加标签,当然Tab在切换时会产生相应的事件,该事件需用OnTabChangedListener监听。
1、定义要显示的布局文件tabhost.xml,如下
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+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"
android:orientation="horizontal"
android:background="@drawable/bg"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tab_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/music"
android:textColor="#f690" />
<TextView
android:id="@+id/tab_vedio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/vedio"
android:textColor="#f690" />
<TextView
android:id="@+id/tab_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/read"
android:textColor="#f690" />
<TextView
android:id="@+id/tab_sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/sport"
android:textColor="#f690" />
</FrameLayout>
</LinearLayout>
</TabHost>
注意在此种布局文件中TabWidget的id需是@android:id/tabs,FrameLayout的id需是@android:id/tabcontent。
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
public class TabBarActivity extends Activity implements OnTabChangeListener
{
private TabHost tabHost;
private TabSpec spec;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tabhost);
// 获得TabHost的对象,并进行初始化setup()
tabHost = (TabHost) this.findViewById(R.id.tabhost);
tabHost.setup();
// 创建选项卡的标签
spec = tabHost.newTabSpec("music");
// 设置该标签页的布局内容
spec.setContent(R.id.tab_music);
// 设置选项卡显示标题
spec.setIndicator("音乐");
tabHost.addTab(spec);
spec = tabHost.newTabSpec("vedio");
spec.setContent(R.id.tab_vedio);
spec.setIndicator("影视");
tabHost.addTab(spec);
spec = tabHost.newTabSpec("read");
spec.setContent(R.id.tab_read);
spec.setIndicator("图书");
tabHost.addTab(spec);
spec = tabHost.newTabSpec("sport");
spec.setContent(R.id.tab_sport);
spec.setIndicator("体育");
tabHost.addTab(spec);
// 可通过setCurrentTab(index)指定显示的页
tabHost.setCurrentTab(0);
tabHost.setOnTabChangedListener(this);
}
// 相应事件的处理
@Override
public void onTabChanged(String tabId)
{
if ("music".equals(tabId))
{
Toast.makeText(TabBarActivity.this, "你点击了" + tabId, Toast.LENGTH_LONG)
.show();
}
// TODO
}
}
实现的效果如下