2017-08-24 3 views
0

J'essaie de créer un TabLayout en XML en tant que page de profil utilisateur. Essentiellement, je veux afficher le texte sur 3 onglets distincts, et le texte sera mis à jour en fonction des données dynamiques dans Firebase. Voici mon code que j'ai, et il ne rend pas mon premier onglet. Je visais essentiellement la conception ci-dessous:Impossible de créer TabLayout en XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/tabcontent"> 

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

       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" > 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="TEST"/> 
       </TabWidget> 
    </TabHost> 

</FrameLayout> 

enter image description here

Répondre

0

Exception déclenchée lors du rendu: TabHost nécessite un FrameLayout avec id "android: id/tabcontent".

Cela signifie que vous devez utiliser FrameLayout à l'intérieur du TabHost, avec l'identifiant indiqué ci-

0

Vous pouvez essayer le nouveau android.support.design.widget.TabLayout, simple et facile à utiliser. Voici un exemple simple, cliquez sur TabItem pour changer de page.

Mise en page

<android.support.design.widget.TabLayout 
    android:id="@+id/main_tablayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:tabIndicatorHeight="4dp" 
    app:tabGravity="fill"> 

    <android.support.design.widget.TabItem 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="table 1"/> 

    <android.support.design.widget.TabItem 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="table 2"/> 

    <android.support.design.widget.TabItem 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="table 3"/> 
</android.support.design.widget.TabLayout> 

<ViewAnimator 
    android:id="@+id/main_viewanimator" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/main_recyclerview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <Button 
     android:id="@+id/main_button_pip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="@string/pip_mode"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="TEST"/> 
</ViewAnimator> 

du code Java

mViewAnimator = findViewById(R.id.main_viewanimator); 
mTabLayout = findViewById(R.id.main_tablayout); 
mTabLayout.addOnTabSelectedListener(mTabListen); 

private final TabLayout.OnTabSelectedListener mTabListen = new TabLayout.OnTabSelectedListener() { 
    @Override 
    public void onTabSelected(TabLayout.Tab tab) { 
     switch (tab.getPosition()) { 
      case 0: 
       mViewAnimator.setDisplayedChild(0); 
       break; 

      case 1: 
       mViewAnimator.setDisplayedChild(1); 
       break; 

      case 2: 
       mViewAnimator.setDisplayedChild(2); 
       break; 
     } 
    } 

    @Override 
    public void onTabUnselected(TabLayout.Tab tab) { 
    } 

    @Override 
    public void onTabReselected(TabLayout.Tab tab) { 
    } 
}; 

Snapshot enter image description here

+0

alors que je pense que votre réponse peut travailler, je suis essentiellement en utilisant les onglets comme XML et je vais placer le texte dynamique à l'intérieur des onglets (il se mettra à jour en fonction de l'interaction avec Firebase). Je n'ai pas vraiment besoin de la fonction recylerview, les onglets ne sont que l'affichage similaire à l'écran du profil. – tccpg288