如何在不扩展TabActivity的情况下显示选项卡视图

时间:2013-01-10 21:31:35

标签: android tabs android-tabhost tabactivity

我正在尝试使用TabHost小部件。我没有意识到TabActivity已被弃用。当我运行我的活动时,我无法在屏幕上显示我的TabHost。目前,我的XML文件包含以下内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<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>

我的MainActivity.java包含:

public class MainActivity extends Activity {

TabHost myTabBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

如何让TabHost出现在屏幕上?每次我google TabHost所有教程都涉及扩展TabActivity!

感谢。

编辑:在做这样的事情时,我是否在正确的轨道上?

myTabBar = (TabHost)findViewById(android.R.id.tabhost);
    myTabBar.setup();

1 个答案:

答案 0 :(得分:0)

标签中的活动已被弃用,Google同意这样做是一个坏主意,您必须考虑使用视图作为标签的内容,here例如CommonsWare的书,您可以找到有用的资源。< / p>

相关问题