Android: Tab的使用总结

本文详细介绍了Android中TabView的使用方法,包括TabHost的基本概念、TabActivity的应用、TabSpec的创建过程及如何通过不同方式添加子标签。同时对比了使用布局文件、TabContentFactory接口及嵌入其他Activity三种添加子标签的方法。

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

之前在 t.sina.com.cn/kevinlynx 陆续贴了些android开发涉及到的一些code snippets,主要目的是记录,供以后需要的时候查阅方便。但是因为miniblog的字数限制,在记录较多信息的时候还是有诸多不便。so, i'm here. 

Tab view算是我接触android平台第二个比较迷惑的地方(第一个是ListView的adapter)。在百度了一些别人的使用心得/总结后,依然发现对tab view的使用存在很多疑问。包括ApiDemo里的例子以及其他一些例子代码。就目前而言我依然存在部分疑点,不过,就单纯的使用而言,倒是足够了。 

Tab view通过TabHost来管理tab page: 
A TabHost is(copyed from official reference): 
Container for a tabbed window view. This object holds two children: a set of tab labels that the user clicks to select a specific tab, and a FrameLayout object that displays the contents of that page. 

使用TabHost必然涉及到TabActivity: 
Java代码   收藏代码
  1. class TabSample extends TabActivity {  
  2.     protected void onCreate( Bundle savedInstanceState ) {  
  3.          TabHost tabHost = getTabHost();  
  4.     }  
  5. };  

取得TabHost 更多地是为tab添加page,即标签页。 

此外,android用TabSpec来表示一个标签页,可以通过tabHost.newTabSpec来创建,创建的时候一般会立即设置各种属性,如下代码: 

Java代码   收藏代码
  1. tabHost.addTab(tabHost.newTabSpec("tab1")  
  2.         .setIndicator("tab1")  
  3.         .setContent(R.id.view1));  



添加标签页的方式就多了,方式太多正是迷惑人的地方。就我目前所查阅的资料来看,有3个方式添加子标签(添加子标签即为tab setContent): 

1、如ApiDemo Tabs1.java所示,使用layout (declared in xml)来作为内容
: 
Java代码   收藏代码
  1. LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);  
  2.   
  3.         tabHost.addTab(tabHost.newTabSpec("tab1")  
  4.                 .setIndicator("tab1")  
  5.                 .setContent(R.id.view1));  

这里的inflate实际上我不太理解,好像是将tabs1这个layout实力化为java对象,并作为tabHost.getTabContentView()的子views。inflat相关机制还没去研究。 

2、如ApiDemo Tabs2.java所示,使用TabHost.TabContentFactory来设置标签页的内容:
 
Java代码   收藏代码
  1. public class Tabs2 extends TabActivity implements TabHost.TabContentFactory {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.   
  7.         final TabHost tabHost = getTabHost();  
  8.         tabHost.addTab(tabHost.newTabSpec("tab1")  
  9.                 .setIndicator("tab1", getResources().getDrawable(R.drawable.star_big_on))  
  10.                 .setContent(this));  
  11.             ....  
  12.  public View createTabContent(String tag) {  
  13.         final TextView tv = new TextView(this);  
  14.         tv.setText("Content for tab with tag " + tag);  
  15.         return tv;  
  16.     }  


3、我想这才是我目前所需要的方式,直接嵌入其他的Activity进来。通过Intent的方式,如ApiDemo Tabs3.java所示:
 
Java代码   收藏代码
  1. protected void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.   
  4.     final TabHost tabHost = getTabHost();  
  5.   
  6.     tabHost.addTab(tabHost.newTabSpec("tab1")  
  7.             .setIndicator("list")  
  8.             .setContent(new Intent(this, List1.class)));  
  9.      ....  



以上几种方式中,只有方式1需要使用一个layout(我的意思是xml配置的)。在我查看的另一份代码中(UROP1,一个闹钟程序),他使用了第三种方式,但却在开头setContentView。在我注释掉这行代码时,效果依然没变。包括在网上的很多其他文章,都会使用setContentView,但看起来有点多余。这也是让我迷惑的地方。

综合来说,三种方式, 都不需要setContentView,方式1需要inflat一份layout,其他2种则不需要。  

BTW,Javaeye的blog编辑功能还不错。:D 

1.2.2011 update 

默认设置下,TabActivity中的content是activity的话,第一次切换会触发onCreate,切换标签页 
不会导致当前activity onStop/onDestroy,只有当退出TabActivity时,才触发。 

标签页切换会导致子activity onPause,只有当前处于前台的activity,才会接收到 
onBackPressed,整个TabActivity退出时,只会触发一次finishedFromChild。 
我要处理一个大显示下的文本长度适配:不选择文件的情况下,在中文语言下显示正常,切换到文本较长的语言并且字体设置超大,显示设置较大后,文本会把按钮挤到屏幕边缘,怎么处理<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/dialog_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="@dimen/dimen_84dp" android:orientation="vertical"> <com.google.android.material.appbar.COUIDividerAppBarLayout android:id="@+id/appbar" style="@style/CommonAppBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/color_transparent" android:clickable="true" android:focusable="true" android:paddingLeft="@dimen/dimen_0dp" android:paddingRight="@dimen/dimen_0dp" app:elevation="@dimen/toolbar_elevation"> <com.coui.appcompat.toolbar.COUIToolbar android:id="@+id/toolbar" style="@style/COUIToolBarInAppBarLayoutStyle" android:layout_width="match_parent" android:background="@null" app:supportTitleTextAppearance="@style/textAppearanceSecondTitle" app:titleCenter="false" /> <com.coui.appcompat.tablayout.COUITabLayout android:id="@+id/tab_layout" android:background="@color/color_transparent" style="@style/COUISmallTabLayoutStyle" /> </com.google.android.material.appbar.COUIDividerAppBarLayout> <RelativeLayout android:id="@+id/content_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <com.filemanager.common.view.ViewPagerWrapperForPC android:id="@+id/view_pager_wrapper" android:layout_width="match_parent" android:layout_height="match_parent"> <com.filemanager.common.view.viewpager.RTLViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/appbar" android:clipChildren="false" android:clipToPadding="false" android:orientation="horizontal" /> </com.filemanager.common.view.ViewPagerWrapperForPC> </RelativeLayout> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?attr/couiColorBackgroundElevatedWithCard" android:minHeight="@dimen/operation_btn_background_height"> <View android:id="@+id/button_divider" android:layout_width="match_parent" android:layout_height="@dimen/divider_background_height" android:layout_gravity="top" android:alpha="0" android:background="?attr/couiColorDivider" android:forceDarkAllowed="false" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <LinearLayout android:id="@+id/select_root_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginStart="@dimen/dimen_16dp" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="false" android:orientation="horizontal"> <TextView android:id="@+id/select_title_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/not_selected_file" android:textSize="16sp" android:lineHeight="22dp" android:textColor="?attr/couiColorLabelPrimary" android:textAppearance="@style/couiTextHeadlineXS" android:visibility="visible"/> <ImageView android:id="@+id/select_arraw_up" android:layout_width="18dp" android:layout_height="18dp" android:src="@drawable/arrow_up" android:layout_marginLeft="@dimen/dimen_6dp" android:visibility="gone" android:layout_gravity="center"/> </LinearLayout> <TextView android:id="@+id/select_body_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:lineHeight="20dp" android:textSize="14sp" android:textAppearance="?attr/couiTextBodyXS" android:textColor="?attr/couiColorLabelSecondary" android:visibility="gone" android:text="@string/selected_size"/> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> <com.coui.appcompat.button.COUIButton android:id="@+id/btn_add_file" style="@style/Widget.COUI.Button.Large" android:layout_width="@dimen/dimen_96dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginEnd="@dimen/dimen_16dp" android:text="@string/label_add_recent_file" /> </LinearLayout> </FrameLayout> </FrameLayout>
最新发布
07-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值