Android TabHost的用法


用Eclipse可视化布局生成的以下句子

<TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <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" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

然后Activity里面的代码

TabHost mTabHost = (TabHost) mView.findViewById(android.R.id.tabhost);
		mTabHost.setup();
		
		TabSpec ts1 = mTabHost.newTabSpec("tab1");
		ts1.setContent(R.id.tab1);
		ts1.setIndicator("互动");// 设置tab名字,可以自定义view
		TabSpec ts2 = mTabHost.newTabSpec("tab2");
		ts2.setContent(R.id.tab2);
		ts2.setIndicator("互动");
		
		mTabHost.addTab(ts1);
		mTabHost.addTab(ts2);

.setup()。

Call setup() before adding tabs if loading TabHost using findViewById(). However: You do not need to call setup() after getTabHost() in TabActivity. Example:

如果使用findViewById()加载TabHost,在添加tab之前调用setup(),如果是继承TabActivity就可以不用.setup()了


你可能感兴趣的:(eclipse,android,layout,tabs)