0

J'ai construit un TabHost dans mon tiroir de navigation. Cela signifie que j'ai créé 3 Fragment avec xml fichiers chacun: ExhibitorFragment, TabInternationalFragment et TabLocalFragment. Maintenant, le problème est, je ne peux pas voir mon tabwidget sous le Toolbar même si elle affiche la conception xml dans deux des fragments.Tabwidget pas visible sous la barre d'outils/navbar dans le studio android

ci-dessous est mon code pour ExhibitorFragment.java:

public class ExhibitorFragment extends Fragment{ 


public ExhibitorFragment() { 
    // Required empty public constructor 
} 

private FragmentTabHost mTabHost; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 

    mTabHost = new FragmentTabHost(getActivity()); 
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.fragment_exhibitor); 

    Bundle arg1 = new Bundle(); 
    arg1.putInt("Arg for Frag1", 1); 
    mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Frag Tab1"), 
      TabInternationalFragment.class, arg1); 

    Bundle arg2 = new Bundle(); 
    arg2.putInt("Arg for Frag2", 2); 
    mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("Frag Tab2"), 
      TabLocalFragment.class, arg2); 


    return mTabHost; 
} 

@Override 
public void onDestroyView() { 
    super.onDestroyView(); 
    mTabHost = null; 
} 

} 

fragment_exhibitor.xml:

<FrameLayout 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.example.guitarista.citem.Example"> 

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

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

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

       > 

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

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


      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 

</FrameLayout> 

Le TabInternationalFragment et TabLocalFragment sont définis par défaut lors de la création même que leur respecté xml (fragment_tab_international, fragment_tab_local).

Répondre

0

Votre code Java est correct Essayez de modifier le fichier XML à ce format

<android.support.v4.app.FragmentTabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_marginTop="55dp" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <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="45dp"/> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</android.support.v4.app.FragmentTabHost> 
+0

J'ai des problèmes de rendu. Est-ce normal? – SovietSenpai

+0

Oui, c'est tout à fait normal .... La mise en page ne sera visible que dans l'exécution – qwertygamer

+0

ne fonctionne toujours pas :( – SovietSenpai

0

Usr ce code

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

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

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

       /> 

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

       > 

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

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


      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 

</FrameLayout> 
+0

Quelle est la différence dans celui-ci? Je ne peux pas sembler – SovietSenpai

+0

android: layout_below = "@ android: id/onglets" –

0
FragmentTabHost mTabHost; 
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); 

    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("tab2"), 
      tab1.class,null); 

    mTabHost.addTab(mTabHost.newTabSpec("tab2") 
      .setIndicator("tab2"), tab2.class, null); 
+0

ill essayer it .. – SovietSenpai

+0

toujours pas de chance frère.peut-être difficile de traduire certains des codes, parce que Im implémentant ceux-ci en fragments au lieu de la MainActivity elle-même. – SovietSenpai

+0

Compilez avec ceci et essayez-le compile 'com.android.support:design:24.2.1' compile 'com.android.support:support-v4:24.2.1' – qwertygamer

0

J'ai suivi ce tutoriel Android Tabs in Fragments. Bien que celui-ci est probablement similaire à la bande d'onglet Pager Sliding. Prenez note que vous devez toujours implémenter des codes de fragments pour que cela fonctionne.