2014-09-28 138 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"/>