【问题标题】:TabHost android isn't working correctlyTabHost android 无法正常工作
【发布时间】:2012-03-29 00:11:30
【问题描述】:

我正在尝试让这个 TabHost 工作。当我将 TabHost 添加到 xml 布局并运行程序时,我可以看到每个选项卡的不同内容堆叠在一起。这告诉我布局工作正常。然后我将我的规范代码添加到活动中,然后在我导航到此屏幕时关闭模拟器上的程序。
所以这是我的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/tWelcome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/bNewTask"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Add Task" />

<TabHost
    android:id="@+id/myTabs"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@+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" >

                <ListView
                    android:id="@+id/listView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" >
                </ListView>
            </LinearLayout>

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

                <TextView
                    android:id="@+id/tTest"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Test"
                    android:textAppearance="?android:attr/textAppearanceLarge" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>
 </TabHost>

</LinearLayout>

现在是我的活动代码:(唯一重要的部分......)

            welcomeName = (TextView) findViewById(R.id.tWelcome);
    Test = (TextView) findViewById(R.id.tTest);
    newTask = (Button) findViewById(R.id.bNewTask);
    myTask = (ListView) findViewById(R.id.listView);
    TabHost th = (TabHost) findViewById(R.id.myTabs);

    welcomeName.setText("Welcome " + ToDoActivity.myUser.getName() + "!");

    th.setup();
    TabSpec specs = th.newTabSpec("tag1");
    specs.setContent(R.id.tab1);
    specs.setIndicator("All");
    th.addTab(specs);
    specs = th.newTabSpec("tag2");
    specs.setContent(R.id.tab2);
    specs.setIndicator("Personal");
    th.addTab(specs);

【问题讨论】:

  • 为什么不使用TabLayout 而不是TabHost

标签: android eclipse listview android-tabhost tabwidget


【解决方案1】:

这是用于标签的 Android 教程页面: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

它有关于选项卡的信息和要求,并提供了大量的示例代码。您应该可以从那里复制粘贴然后进行编辑。

具体来说,我会将所有 Tab 元素的 id 更改为建议的 Android id:

android:id/tabhost
android:id/tabs
android:id/tabcontent

【讨论】:

    【解决方案2】:

    我想到了几种可能性。

    如果您使用的是 TabActivity(不是必需的,但可以让事情变得更容易),tabhost 必须有 android:id="@android:id/tabhost" 作为 id。

    我也看到它说tabhost必须是根节点。我无法确认使用 android 文档。在所有其他途径都失败(或有人确认这是真的)之后,我可能会做出这种改变。

    如果您使用的是 TabActivity,则无需通过 id 引用 tabhost,也无需使用 setup() 方法。我看到您正在使用 setup() 方法,但我不知道您的活动是否是 TabActivity。如果是,那可能是您的问题的原因。

    以下是我用于设置选项卡布局的方法,它适用于我。请注意,这假定 tabhost 作为布局的根节点,它的 id 为 android:id/tabhost,它是一个 TabActivity。

    标签宿主代码:

    public class Tabs extends TabActivity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.your_layout);
    
            TabHost tabHost = getTabHost();
            TabHost.TabSpec spec;
    
            spec = tabHost.newTabSpec("tag1").setIndicator("All").setContent(R.id.tab1);
            tabHost.addTab(spec);
            spec = tabHost.newTabSpec("tag2").setIndicator("Personal").setContent(R.id.tab2);
            tabHost.addTab(spec);
        }
    }
    

    希望这会有所帮助! (如果它解决了您的问题,请务必单击复选标记以接受答案)。

    【讨论】:

    • getTabHost() 不是我定义的方法。我是否让此方法返回 TabHost Id 或其他内容?
    • 如果你扩展 TabActivity 它是一个方法。
    • 请记住 TabActivity 已被弃用。我会使用 ActionBarSherlock actionbarsherlock.com 并仅使用新的 ActionBar API 来创建选项卡。这让一切变得更容易,尤其是转向冰淇淋三明治,这将在接下来的半年里获得很多新设备......
    • 修改了我的答案,使其更加清晰。检查here 以获取有关通过扩展 TabActivity 类可用的方法的更多信息。尽管应该注意的是,他们已经弃用它而支持使用片段。我还没有采取行动,所以如果你想这样做,我帮不上什么忙。
    • 我能否在扩展 TabActivity 的同时让 TabHost 中没有的所有其他功能正常工作?
    猜你喜欢
    • 2017-03-10
    • 2012-09-13
    • 2012-03-24
    • 2012-11-03
    • 2013-11-24
    • 1970-01-01
    • 2014-09-17
    • 2011-07-04
    • 2015-10-04
    相关资源
    最近更新 更多