在上一篇Android的Tab控件(一)中,各个tabPage都由独立的Activity来实现。然而TabHost还有另外一种实现方法。接下来就来讲解,在实现的时候我附加了一个功能,就是当tab页太多的时候,默认情况下tab页会缩小挤在一起,我们给TabWidget加一个HorizontalScrollView,就可以实现TabWidget的左右滚动。先来看效果图;
Activity代码:
public class ScrollTabActivity extends Activity{
Intent intent;
TabHost.TabSpec tabSpec;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.scrolltab);
Resources res=getResources();
// 获取TabHost对象
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
// 如果没有继承TabActivity时,通过该种方法加载启动tabHost
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("第一个标签",
getResources().getDrawable(R.drawable.image)).setContent(
R.id.view1));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二个标签")
.setContent(R.id.view2));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三个标签")
.setContent(R.id.view3));
tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("第四个标签",
getResources().getDrawable(R.drawable.image)).setContent(
R.id.view4));
tabHost.addTab(tabHost.newTabSpec("tab5").setIndicator("第五个标签")
.setContent(R.id.view5));
tabHost.addTab(tabHost.newTabSpec("tab6").setIndicator("第六个标签")
.setContent(R.id.view6));
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/hometabs"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- TabHost必须包含一个 TabWidget和一个FrameLayout-->
<TabHost android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<!-- TabWidget的id属性必须为 @android:id/tabs-->
<TabWidget android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TabWidget>
</HorizontalScrollView>
<!-- FrameLayout的id属性必须为 @android:id/tabcontent-->
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/view1"
android:text="diyige "
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TableLayout
android:id="@+id/view2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:id="@+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="密码" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1" />
</TableRow>
</TableLayout>
<TextView android:id="@+id/view3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/view4"
android:text="diyige "
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/view5"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/view6"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
</LinearLayout>
</TabHost>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hha"/>
</LinearLayout>
另外,如果想让标签页显示在最下面,只需要交换TabWidget和FrameLayout 的位置就行了。