Android TabHost布局

Android中使用TabHost 可以在一个屏幕间进行不同版面的切换:
在Android中要实现Tab切换页面的效果,大致有两种方法:一是利用layout下的xml配置来实现tab的布局,二是动态加载tabHost进行布局。

 1、利用xml文件配置,引用xml的activity不用继承于TabActivity,通过findViewById方法获取TabHost,

具体的xml见下:

<?xml version="1.0" encoding="utf-8"?>
<TabHost android:id="@+id/tabhost" android:layout_width="fill_parent"
	android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
	<LinearLayout android:orientation="vertical"
		android:layout_width="fill_parent" android:layout_height="fill_parent">
                <!-- this is tabs content -->
		<FrameLayout android:id="@android:id/tabcontent"  
			android:layout_width="fill_parent" android:layout_height="wrap_content"
			android:paddingBottom="62px">
			
			<!-- tab1 content -->
			<LinearLayout android:id="@+id/content1"
				android:layout_width="fill_parent" android:layout_height="fill_parent"
				android:orientation="vertical">
				<!--省略若干代码-->
			</LinearLayout>

                        <!-- tab2 content -->
			<LinearLayout android:id="@+id/content2"
				android:layout_width="fill_parent" android:layout_height="fill_parent"
				android:orientation="vertical">
			       <!--省略若干代码-->
			</LinearLayout>
		</FrameLayout>
		
		<RelativeLayout  
                            android:layout_width="fill_parent"  
                            android:layout_height="fill_parent">  
                           <TabWidget  
                		android:id="@android:id/tabs"  
                		android:layout_alignParentBottom="true"  
                		android:layout_width="fill_parent"  
                		android:layout_height="wrap_content" />  
       	    </RelativeLayout> 
	</LinearLayout>
</TabHost>


    xml中TabHost,FrameLayout,TabWidget 元素都要引用系统的id,若改用其他id会抛异常的,绿色的部分可以自定义

    Activity部分代码:

		setContentView(R.layout.***); //设置上面的xml文件
                TabHost tabhost = (TabHost) findViewById(R.id.tabhost);
		tabhost.setup();   // 初始化TabHost容器
		// tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1", null).setContent(R.id.content1));
		TabHost.TabSpec tab_1 = tabhost.newTabSpec("tab_1"); // 设置一个tab
		tab_1.setContent(R.id.content1); // 设置tab里面的布局
		tab_1.setIndicator("按地名查询"); // 设置tab的标题和图表
		tabhost.addTab(tab_1); // 在tabhost上添加新的tab
		// tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("tab2", null).setContent(R.id.content2));
		TabHost.TabSpec tab_2 = tabhost.newTabSpec("tab_2");
		tab_2.setContent(R.id.content2);
		tab_2.setIndicator("按起点终点查询");
		tabhost.addTab(tab_2);
		// 设置默认显示第几个tab
		tabhost.setCurrentTab(0);



2、继承TabActivity 类,,Activity中使用getTabHost()获得TabHost,使用android的自己内部定义好的.xml资源文件作容器文件,进行动态布局;

    tab_search.xml,用来布局tabHost下的content

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tab_hs_id">
    
    <TextView android:id="@+id/tab_search_tv"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent"/>
    	
</FrameLayout>

    Activity中的代码:


		tabHost = getTabHost();
                LayoutInflater inflater_tab3 = LayoutInflater.from(this);
		inflater_tab3.inflate(R.layout.tab_search, tabHost.getTabContentView());
                /*
		 * R.layout.tab_search已被LayoutInflater注册,所以这个content可以通过findViewById获得其对象
		 */
		TabHost.TabSpec tabSpec_near = tabHost.newTabSpec("nearest");
		tabSpec_near.setIndicator("Nearest", null);
		tabSpec_near.setContent(R.id.tab_hs_id); // 绑定一个新的Layout
		tabHost.addTab(tabSpec_near);  //添加一个tab
	       setContentView(tabHost);  //设置avtivity的布局为tabHost
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TabHostAndroid 应用程序中常用的选项卡控件,用于在多个选项卡之间切换。以下是 Android Studio 中使用 TabHost 控件的步骤: 1. 在布局文件中添加 TabHost 控件 ``` <TabHost android:id="@+id/tab_host" 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" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 添加选项卡内容布局 --> <LinearLayout android:id="@+id/tab1_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tab 1" /> </LinearLayout> <LinearLayout android:id="@+id/tab2_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tab 2" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> ``` 2. 在 Java 代码中获取 TabHost 控件并设置选项卡 ```java TabHost tabHost = findViewById(R.id.tab_host); tabHost.setup(); // 添加选项卡 TabHost.TabSpec tab1 = tabHost.newTabSpec("Tab1"); tab1.setIndicator("Tab 1"); tab1.setContent(R.id.tab1_layout); tabHost.addTab(tab1); TabHost.TabSpec tab2 = tabHost.newTabSpec("Tab2"); tab2.setIndicator("Tab 2"); tab2.setContent(R.id.tab2_layout); tabHost.addTab(tab2); ``` 在以上代码中,我们首先获取 TabHost 控件,并通过 `setup()` 方法初始化。然后,我们使用 `newTabSpec()` 方法创建选项卡,并设置选项卡的标签和内容布局。最后,我们使用 `addTab()` 方法将选项卡添加到 TabHost 控件中。 以上就是使用 TabHost 控件的基本步骤。你可以根据自己的需要自定义选项卡的样式和内容。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值