【问题标题】:How to display tab view without extending TabActivity如何在不扩展 TabActivity 的情况下显示选项卡视图
【发布时间】:2013-01-10 21:31:35
【问题描述】:

我正在尝试使用 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 出现在屏幕上?每次我在谷歌上搜索 TabHost 时,所有教程都涉及扩展 TabActivity!

谢谢。

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

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

【问题讨论】:

    标签: android tabs android-tabhost tabactivity


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多