剖析下Android里面的Tab
剖析下Android里面的Tab
今天研究了TabView但是发现一个奇怪的现象,从无数网上的代码来看,TabView的呈现都有相关的布局,好吧,先就布局来说,有2个样子,一个是TabView置顶,一个是至底,要说这2个有什么不同的话,其实也就布局的不同。先看看置顶的布局
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:Android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/tabhost"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- </FrameLayout>
- </LinearLayout>
- </TabHost>
下面再看看置低的布局
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/tabhost"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </FrameLayout>
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- </TabHost>
仔细研究下就可以看出来区别了仅仅是<TabWidget>和<FrameLayout>的位置不同而已。问题似乎就此也就结束了,然而,你在android SDk提供的相关列子APIdemos你会发现其源码里面根本就没有所谓的什么<TabHost><TabWidget>。这是什么原因?而且它也可以正常的运行。我先贴一个代码,你完全可以自己试试看,是不是用不着布局呢?
- public class Tabs3 extends TabActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- final TabHost tabHost = getTabHost();
- tabHost.addTab(tabHost.newTabSpec("tab1")
- .setIndicator("list")
- .setContent(new Intent(this, List1.class)));
- tabHost.addTab(tabHost.newTabSpec("tab2")
- .setIndicator("photo list")
- .setContent(new Intent(this, List8.class)));
- // This tab sets the intent flag so that it is recreated each time
- // the tab is clicked.
- tabHost.addTab(tabHost.newTabSpec("tab3")
- .setIndicator("destroy")
- .setContent(new Intent(this, Controls2.class)
- .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
- }
- }
当然这里面用到了Intent的跳转,似乎也就用不是布局了,这不是重点。重点是所谓的TabHost为什么不需要?目前作者还没想明白,有精通的大哥能否告诉我呢。。。
最后再说一件有意思的事情,目前我看到的有这种Tab效果的已经发现有好几个不是用的TabHost来做的,当然,玩转四方还是用的这个。
评论暂时关闭