今天我们来写一个TabHost的实例,先来一张效果图:
主要代码:
1. res/ layout/ activity_main.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</TabWidget>
</RelativeLayout>
</TabHost>2. src/ MainActivity.java
package com.example.demo_ui7_tabwdget;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TabActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.widget.TabHost;
import android.widget.Toast;
@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
private Context ctx = this;
private TabHost tabhost;
private Intent intentStudy;
private Intent intentScan;
private Intent intentReview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intentStudy = new Intent(MainActivity.this, StudyActivity.class);
intentScan = new Intent(MainActivity.this, ScanActivity.class);
intentReview = new Intent(MainActivity.this, ReviewActivity.class);
tabhost = getTabHost();
tabhost.addTab(tabhost
.newTabSpec("tab1")
.setIndicator("Tab1",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intentStudy));
tabhost.addTab(tabhost
.newTabSpec("tab2")
.setIndicator("Tab2",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intentScan));
tabhost.addTab(tabhost
.newTabSpec("tab3")
.setIndicator("Tab3",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intentReview));
tabhost.setCurrentTab(0);
tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
Toast.makeText(ctx, ""+tabId, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这里只贴出了MainActivity和activity_main布局文件, 宁外用到了 StudyActivity, ScanActivity, ReviewActivity 就是三个普通的activity,这里就不贴代码了,请大家自行添加。
我们在使用TabHost的时候一定要注意,一个应用中只能有一个TabHost组件,并且其获取方式也和其他的组件不同。我们来看一看:
tabhost = getTabHost(); //直接通过系统得到
添加tab的方式:
tabhost.addTab(tabhost
.newTabSpec("tab1")
.setIndicator("Tab1",getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intentStudy));这里我把它整理一下,这样好看一点。
newTabSpec("tab1") :"tab1"将作为tabId
setIndicator(“Tab1", getResource().getDrawable(R.drawable.ic_launcher) :设置tab的标题,以及icon(但是这里好像icon没有起作用)
setContent( intent ) : 这里传入 intent对象 ,说明切换tab的时候,显示的是intent对象所跳转到的activity。
然后这里在写layout的时候有几点一定要注意:
TabHost的id, FramLayout的id, TabWidget的id:
android:id="@android:id/tabhost"android:id="@android:id/tabcontent"android:id="@android:id/tabs"这三个id的写法和普通的不一样: android:id="@+id/ ..." (普通)。
如果你在写实例的时候发现app运行错误,报 tabhost找不到, tabcontent找不到, 或tabs找不到,那么多半是这里出了问题。
好了,实例写到这里,完!
本文详细介绍了一个使用TabHost创建标签页导航的实例。包括MainActivity及布局文件的代码实现,讲解了如何添加标签、设置标签指示器及内容。同时指出在布局文件中正确设置TabHost组件ID的重要性。

1686

被折叠的 条评论
为什么被折叠?



