Android的Tab控件(二)

本文介绍了一种使用TabHost与HorizontalScrollView结合的方法,以实现Android应用中TabWidget的滑动功能。通过在TabHost中嵌套HorizontalScrollView,可以解决Tab数量过多导致的显示问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在上一篇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 的位置就行了。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值