2013-08-30 4 views
6

actuellement j'essaye d'implémenter le FragmentTabHost pour mon projet. Je suis encore nouveau sur ces fragments mais je l'ai trouvé très bien en termes de réutilisation des mises en page, etc. C'est pourquoi j'ai voulu me pousser plus loin. Maintenant, je lis les tutoriels sur la façon de créer un onglets avec le fragment et je suis tombé sur ce tutoriel: Maintenant, cela fonctionnePersonnaliser FragmentTabHost mettre TabWidget sur le fond

http://maxalley.wordpress.com/2013/05/18/android-creating-a-tab-layout-with-fragmenttabhost-and-fragments/

très bien, sauf que le tabWidget est au-dessus de ma mise en page où je voulais que ça être en bas. Je trouve que je dois installer le tabWidget après tous les onglets a été initialisées j'ai donc essayé d'ajouter ces codes:

mTabWidget = (TabWidget) findViewById(android.R.id.tabs); 

    mTabWidget.setBackgroundColor(Color.WHITE); 
    mTabWidget.setShowDividers(LinearLayout.SHOW_DIVIDER_NONE); 
    mTabWidget.setGravity(Gravity.BOTTOM); 

Maintenant, celui-ci déjà éliminé le diviseur et change la couleur, mais de toute évidence ne sera pas mis mon Widget sur la partie inférieure de ma mise en page. Maintenant, comment ferais-je cela?

J'ai également essayé d'éditer le xml Tabhost et juste mettre TabWidget après le FrameLayout mais rien ne se passe. voici le xml:

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

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

       <FrameLayout 
         android:id="@+id/tabFrameLayout" 
         android:layout_width="match_parent" 
         android:layout_height="0dp" 
         android:layout_weight="1" /> 

       <TabWidget 
         android:id="@android:id/tabs" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="0" 
         android:orientation="horizontal" 
         /> 

      </LinearLayout> 

     </android.support.v4.app.FragmentTabHost> 

Répondre

13

Je consulter ce lien github example

Ce sera votre mise en page:

<?xml version="1.0" encoding="utf-8"?> 

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

    <FrameLayout 
     android:id="@+id/realtabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" /> 

    <android.support.v4.app.FragmentTabHost 
     android:id="@android:id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

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

</LinearLayout> 

Pour vos personnalisés onglets:

mTabHost.addTab(setIndicator(mTabHost.newTabSpec("Tab1"), 
         R.drawable.image1), 

    public TabSpec setIndicator(Context ctx,TabSpec spec, int resid) { 
     // TODO Auto-generated method stub 
     View v = LayoutInflater.from(ctx).inflate(R.layout.tabs_text, null); 
     v.setBackgroundResource(resid); 
     TextView text = (TextView) v.findViewById(R.id.tab_title); 
     text.setText(spec.getTag()); 
     return spec.setIndicator(v); 
    } 

Modifier

//To set drawable to your perticular TAB 
mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tab_login); 

fin Modifier

Si vous souhaitez ajouter drawable (sélection):

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/tab_compose_h" android:state_selected="true"/> 
    <item android:drawable="@drawable/tab_compose_h" android:state_pressed="true"/> 
    <item android:drawable="@drawable/tab_compose"/> 

</selector> 
+0

en train de vérifier dehors. :) – KaHeL

+0

Salut Gru, testé et oui ça marche! Mais j'ai un problème. Le tabDividers montre et je ne sais pas pour le déclencher. ainsi que je ne peux pas changer la couleur des onglets. Comment devrais-je faire ça? – KaHeL

+0

Et je l'ai déjà eu! :)) Merci beaucoup! – KaHeL

Questions connexes