2010-07-02 11 views
8

Android 2.2 i.e API Livello 8 ha tabStripEnabled = "true" per TabWidget come ottenere lo stesso nelle versioni precedenti di Android?tabStripEnabled per TabWidget nelle API precedenti

+0

ho avuto la schede nella parte inferiore dello schermo, Così ho fatto come qui sotto ... ho messo android: layout_marginBottom = "- 10dip" spostando il bottomStrip fuori dallo schermo ma desidero essere il modo esatto per farlo ... grazie – amithgc

risposta

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

Grazie mille!!!! – Eby

+0

Questo non ha funzionato per me. Ho provato entrambi su un emulatore 2.1 e 2.2. C'è qualcos'altro che dovrei considerare quando uso questo hack? Ha eseguito correttamente il codice in base all'SDK corrente ma il bordo inferiore per TabWidget è rimasto. – dannyroa

+1

Ciò ha funzionato perfettamente, alcune cose da notare, creare un'immagine trasparente e nominarla vuota. Ho apportato una leggera modifica commentando: LinearLayout ll = (LinearLayout) tabHost.getChildAt (0); TabWidget tw = (TabWidget) ll.getChildAt (0); e in sostituzione con TabWidget tw = tabHost.getTabWidget(); – Fred

0

ho fatta così:

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(); 
    } 
Problemi correlati