【问题标题】:TabHost add tabs dynamicallyTabHost 动态添加标签
【发布时间】:2014-05-05 10:14:35
【问题描述】:

我有一个TabHost,我想动态创建标签。我看过很多例子,但它们不起作用。

TabHost tabHost;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
tab1.setIndicator("First Tab");
tab1.setContent(new Intent(this,MainActivity2.class));
tabHost.addTab(tab1);

这是我尝试过的最后一件事,但它不起作用。这是我考虑过的一些链接。我找不到我做错了什么。

Example1

Example2

<TabHost 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="com.LH.tabletres.MainActivity">
    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:orientation="vertical"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_height="wrap_content"
            android:layout_width="fill_parent">
        </TabWidget>

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

    </LinearLayout>
</TabHost>

【问题讨论】:

  • 为什么不工作?给我简要信息
  • @Blast 我建议你现在应该切换到 ActionBar 选项卡。
  • 我不知道。当我尝试运行它时,android stuido 中有很多错误。其中一些在这里 Caused by: java.lang.NullPointerException at com.LH.tabletres.MainActivity.onCreate(MainActivity.java:18) ` at android.app.Activity.performCreate(Activity.java:5008)` Line:18 包括 tabHost.setup();
  • @Blast 显示你的布局,activity_main.xml
  • @nikis 我已经添加了 activty_main.xml

标签: android android-layout android-studio


【解决方案1】:

我认为您忘记将android:id="@android:id/tabhost" 设置为您的tabhost。如下所示

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >........
.....
</TabHost>

【讨论】:

  • tabHost = (TabHost)findViewById(R.id.tabhost); 抛出错误。还有android.attemptone.com/layouts/dynamic-tabs 在这个链接中他们使用了android.R.id.tabhost
  • 我试过了。我已设置 id 并将代码更改为 R.id.tabhost 仍然无法正常工作:/
  • @Blast 您只需在您的TabHost 中设置此ID android:id="@android:id/tabhost"。就是这样
【解决方案2】:

我认为你必须写

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

所以把@android:id改成@+id

希望对你有帮助。

【讨论】:

    【解决方案3】:

    使用此代码:

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    
    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <HorizontalScrollView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:fillViewport="true"
                android:scrollbars="none">
    
                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:tabStripEnabled="false">
    
                </TabWidget>
    
            </HorizontalScrollView>
    
            <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"
                    android:background="#FFFFFF"
                    android:orientation="vertical">
    
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
    

        final TabHost tabs = (TabHost) findViewById(R.id.tabhost);
        tabs.setup();
        tabs.setCurrentTab(0);
    
        for (int i = 0; i < 5; i++) {
            TabHost.TabSpec spec1 = tabs.newTabSpec("tag1");
    
            spec1.setContent(new TabHost.TabContentFactory() {
                public View createTabContent(String tag) {
                    return (new AnalogClock(MainActivity.this));
                }
            });
            spec1.setIndicator("Clock");
            tabs.addTab(spec1);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多