现在虽然TabHost很少用了,但很多项目仍然在用,值得了解下。
方式一:使用TabHost组件,
1.布局文件
<LinearLayout xmlns:android=" http://schemas.android.com/apk/res/android"
xmlns:tools=" http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TabHost
android:id="@+id/tabhost1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
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="0px"
android:layout_weight="6" >
<!-- 第一个tab的布局 -->
<RelativeLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_centerInParent="true"
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab" />
</RelativeLayout>
<!-- 第二个tab的布局 -->
<RelativeLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_centerInParent="true"
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab2" />
</RelativeLayout>
<!-- 第三个tab的布局 -->
<RelativeLayout
android:id="@+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_centerInParent="true"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab3" />
</RelativeLayout>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</TabWidget>
</LinearLayout>
</TabHost>
</LinearLayout>
2.Activity文件
public class TabActivity1 extends Activity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_1);
mTabHost = (TabHost) findViewById(R.id.tabhost1);
//1.初始化TabHost容器
mTabHost.setup();
//2.在TabHost创建标签,然后设置:标题/图标/标签页布局
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("标签1",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("标签2",null).setContent(R.id.tab2));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("标签3",null).setContent(R.id.tab3));
}
}
public class TabActivity1 extends Activity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_1);
mTabHost = (TabHost) findViewById(R.id.tabhost1);
//1.初始化TabHost容器
mTabHost.setup();
//2.在TabHost创建标签,然后设置:标题/图标/标签页布局
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("标签1",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("标签2",null).setContent(R.id.tab2));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("标签3",null).setContent(R.id.tab3));
}
}
方式二: tab项分开
1。
public class TabActivity2 extends Activity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab_2);
mTabHost = (TabHost) findViewById(R.id.tabhost2);
//1.初始化TabHost容器
mTabHost.setup();
//把tab组件载入到tabHost父窗口中
LayoutInflater i=LayoutInflater.from(this);
i.inflate(R.layout.tab_1, mTabHost.getTabContentView());
i.inflate(R.layout.tab_2, mTabHost.getTabContentView());
i.inflate(R.layout.tab_3, mTabHost.getTabContentView());
//在TabHost创建标签,然后设置:标题/图标/标签页布局
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("标签1").setContent(R.id.tab1));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("标签2").setContent(R.id.tab2));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("标签3").setContent(R.id.tab3));
}
}
3. 使用TabActivity
public class TabActivity3 extends TabActivity {
private TabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTabHost = getTabHost();
LayoutInflater.from(this).inflate(R.layout.activity_tab_3,
mTabHost.getTabContentView(), true);
//在TabHost创建标签,然后设置:标题/图标/标签页布局
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("标签1").setContent(R.id.tab1));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("标签2").setContent(R.id.tab2));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("标签3").setContent(R.id.tab3));
}
}