2010-07-02 4 views
8

Android 2.2 i.e API niveau 8 a tabStripEnabled = "true" pour TabWidget comment faire la même chose dans les anciennes versions d'Android?tabStripEnabled pour TabWidget dans les anciennes API

+0

j'avais les onglets en bas de l'écran, donc je l'ai fait comme ci-dessous ... je mets android: layout_marginBottom = "- 10dip" déplacer le bottomStrip hors de l'écran mais que vous voulez savoir la manière exacte de le faire ... grâce – amithgc

Répondre

8
private void SetupTabs(TabHost tabHost) { 

    LinearLayout ll = (LinearLayout) tabHost.getChildAt(0); 
    TabWidget tw = (TabWidget) ll.getChildAt(0); 

    Field mBottomLeftStrip; 
    Field mBottomRightStrip; 

    try { 
     mBottomLeftStrip = tw.getClass().getDeclaredField("mBottomLeftStrip"); 
     mBottomRightStrip = tw.getClass().getDeclaredField("mBottomRightStrip"); 

     if (!mBottomLeftStrip.isAccessible()) { 
      mBottomLeftStrip.setAccessible(true); 
     } 

     if (!mBottomRightStrip.isAccessible()) { 
      mBottomRightStrip.setAccessible(true); 
     } 

     mBottomLeftStrip.set(tw, getResources().getDrawable(R.drawable.blank)); 
     mBottomRightStrip.set(tw, getResources().getDrawable(R.drawable.blank));// blank is the name of the image in drawable folder 

    } 
    catch (java.lang.NoSuchFieldException e) { 
     // possibly 2.2 
     try { 
      Method stripEnabled = tw.getClass().getDeclaredMethod("setStripEnabled", boolean.class); 
      stripEnabled.invoke(tw, false); 

     } 
     catch (Exception e1) { 
      e1.printStackTrace(); 
     } 
    } 
    catch (Exception e) {} 
} 
+0

Merci beaucoup!!!! – Eby

+0

Cela n'a pas fonctionné pour moi. Je l'ai essayé à la fois sur un émulateur 2.1 et 2.2. Y at-il autre chose que je devrais considérer en utilisant ce hack? Il a correctement exécuté le code basé sur le SDK actuel, mais la bordure inférieure pour le TabWidget est restée. – dannyroa

+1

Cela a fonctionné parfaitement, certaines choses à noter, créer une image transparente et nommez-le vide. J'ai fait un léger changement en commentant: LinearLayout ll = (LinearLayout) tabHost.getChildAt (0); TabWidget tw = (TabWidget) ll.getChildAt (0); et de le remplacer par TabWidget tw = tabHost.getTabWidget(); – Fred

0

je l'ai fait alors:

try { 
     Method setStripEnabled = tabWidget.getClass().getDeclaredMethod(
       "setStripEnabled", boolean.class); 
     setStripEnabled.invoke(tabWidget, true); 

     Method setLeftStripDrawable = tabWidget.getClass() 
       .getDeclaredMethod("setLeftStripDrawable", int.class); 
     setLeftStripDrawable.invoke(tabWidget, R.drawable.tab_line); 

     Method setRightStripDrawable = tabWidget.getClass() 
       .getDeclaredMethod("setRightStripDrawable", int.class); 
     setRightStripDrawable.invoke(tabWidget, R.drawable.tab_line); 
    } catch (NoSuchMethodException e) { 
     e.printStackTrace(); 
    } catch (IllegalArgumentException e) { 
     e.printStackTrace(); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } catch (InvocationTargetException e) { 
     e.printStackTrace(); 
    } 
Questions connexes