2017-02-15 116 views
2

问候我的开发人员。 我是在我使用TabHost和2片里面我想使用另一种TabHost Android项目中的工作, 到目前为止,我已经完成了这个 我的主要TabHost XML是TabHost内的TabHost选项卡

<TabHost 
    android:id="@+id/homeTabHost" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:orientation="vertical"> 

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

       <!--first tab content--> 
       <include layout="@layout/content_blood_glucose_diary_home" /> 

       <!--second tab content--> 
       <include layout="@layout/content_blood_glucose_diary_summary" /> 

       <!--3rd tab content--> 
       <include layout="@layout/content_blood_glucose_diary_activities" /> 

      </FrameLayout> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

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

和我的Java代码这是

private void loadTabs(int tabhost, String tab1, String tab2, String tab3){ 
    TabHost host = (TabHost)findViewById(tabhost); 
    host.setup(); 

    loadTab(host, R.id.homeTab1, tab1); //tab 1 

    loadTab(host, R.id.homeTab2, tab2); //tab 2 

    loadTab(host, R.id.homeTab3, tab3); //tab 3 
} 

private void loadTab(TabHost host, int tabid, String tab){ 
    TabHost.TabSpec spec = host.newTabSpec(tab); 
    spec.setContent(tabid); 
    spec.setIndicator(tab); 
    host.addTab(spec); 
} 

这我从我的onCreate

loadTabs(R.id.homeTabHost, "Today", "Summary", "Activity"); 

了在此点工作的事情打电话马丽娟 但是当我的总结XML再添tabhost(像这样)

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="horizontal" 
    android:weightSum="1"> 

    <TabHost 
     android:id="@+id/summaryTabHost" 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.8"> 

     <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"> 

       <!--first tab content--> 
       <LinearLayout 
        android:id="@+id/summaryTab1" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="tab1" /> 
       </LinearLayout> 

       <!--second tab content--> 
       <LinearLayout 
        android:id="@+id/summaryTab2" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="tab2" /> 
       </LinearLayout> 

       <!--3rd tab content--> 
       <LinearLayout 
        android:id="@+id/summaryTab3" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="tab3" /> 
       </LinearLayout> 

       <!--4th tab content--> 
       <LinearLayout 
        android:id="@+id/summaryTab4" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="tab4" /> 
       </LinearLayout> 
      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 
</LinearLayout> 

,并在我的Java文件中我添加

private void loadTabs(int tabhost, String tab1, String tab2, String tab3, String tab4){ 
    TabHost host = (TabHost)findViewById(tabhost); 
    host.setup(); 

    loadTab(host, R.id.summaryTab1, tab1); //tab 1 

    loadTab(host, R.id.summaryTab2, tab2); //tab 2 

    loadTab(host, R.id.summaryTab3, tab3); //tab 3 

    loadTab(host, R.id.summaryTab4, tab4); //tab 4 
} 

这在我的OnCreate函数,它是

loadTabs(R.id.summaryTabHost, "All", "Yearly", "Monthly", "Weekly"); 

这应该工作... ,因为它与第一个标签主机一样..第一个tabhost选项卡不显示yup ..

但是,当我评论第2个tabhost摘要XML内..东西看起来很正常。

到目前为止,我的诊断是可能有些东西不能与第二个TabHost坐在一起。

任何想法可能是错误的地方?任何帮助被承认!

+0

......我在屏幕底部创建tabhost,但如果我让它在屏幕顶部两个tabhosts工作就好了..它也工作正常,如果我创建第一tabhost顶部的屏幕和内部tabhost底部的屏幕它也工作.. 它似乎第一tabhost只有disapears当我尝试照我的问题行事 –

回答

0

TabHost是Depriected可以使用tablayout 如果你想使标签内的标签,你可以很容易地在这个例子中使用它的内部片段

Androidhive tablayout tutorial

相关问题