2014-09-28 139 views
0

添加tabhost和tabcontent后无法创建tabview。 如何在android中添加选项卡。我使用下面的代码来显示选项卡。不显示标签

XML文件:

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

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

     <LinearLayout 
      android:id="@+id/homeLinearLayout" 
      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" > 

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

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

         <Button 
          android:id="@+id/button1" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Dynamic" /> 
        </LinearLayout> 

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

         <Button 
          android:id="@+id/button2" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:text="Static" /> 
        </LinearLayout> 
       </FrameLayout> 
      </TabWidget> 
     </LinearLayout> 
    </TabHost> 

</LinearLayout> 

Java文件

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home_view); 
    tabHost = (TabHost) findViewById(android.R.id.tabhost); 
    tabHost.setup(); 

    tabSpec = tabHost.newTabSpec("dynamicTab"); 
    tabSpec.setIndicator("Create Image"); 
    tabSpec.setContent(R.id.dynamicLinearLayout); 
    tabHost.addTab(tabSpec); 

    tabSpec = tabHost.newTabSpec("staticTab"); 
    tabSpec.setIndicator("Select Image"); 
    tabSpec.setContent(R.id.staticLinearLayout); 
    tabHost.addTab(tabSpec); 

    tabHost.setCurrentTab(0); 
} 

输出是: enter image description here

无法加载两个标签。 请显示我犯的错误。

+0

你需要给一个不同的名字ta来自不同的前一个像这个tabSpec2的bhost – sakir 2014-09-28 12:48:44

回答

0
tabSpec = tabHost.newTabSpec("dynamicTab"); 
    tabSpec.setIndicator("Create Image"); 
    tabSpec.setContent(R.id.dynamicLinearLayout); 
    tabHost.addTab(tabSpec); 

    tabSpec2 = tabHost.newTabSpec("staticTab"); 
    tabSpec2.setIndicator("Select Image"); 
    tabSpec2.setContent(R.id.staticLinearLayout); 
    tabHost2.addTab(tabSpec2); 

    tabHost.setCurrentTab(0); 
0

的Java部分sakir的答案是正确的,你可以使用它,或者用我的:

super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 
    tabHost.setup(); 

    TabHost.TabSpec tabSpec = tabHost.newTabSpec("dynamicTab"); 
    tabSpec.setIndicator("Create Image"); 
    tabSpec.setContent(R.id.dynamicLinearLayout); 
    tabHost.addTab(tabSpec); 

    TabHost.TabSpec tabSpec1 = tabHost.newTabSpec("staticTab"); 
    tabSpec1.setIndicator("Select Image"); 
    tabSpec1.setContent(R.id.staticLinearLayout); 
    tabHost.addTab(tabSpec1); 

    tabHost.setCurrentTab(0); 

为XML的一部分,你必须删除</TabWidget>和变化:

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

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