2010-07-02 61 views
8

Android 2.2即API Level 8擁有tabWidget的tabStripEnabled =「true」 如何在老版本的Android中實現相同?TabStripEnabled for TabWidget in Old API's

+0

我在屏幕底部的標籤, 所以我做了如下...... 我把機器人:layout_marginBottom = 「 - 10dip」 移動bottomStrip關閉屏幕 但想知道確切的方式來做到這一點... 感謝 – amithgc 2010-07-02 11:46:50

回答

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

非常感謝你!!!! – Eby 2010-10-18 12:05:45

+0

這不適合我。我在2.1和2.2模擬器上都試過。使用這種黑客時還有什麼我應該考慮的? 它正確執行基於當前SDK的代碼,但仍保留TabWidget的底部邊界。 – dannyroa 2010-12-24 05:55:08

+1

這工作完美,有些事情要注意,創建一個透明的圖像,並將其命名爲空白。我通過發表評論做了一些小改動: LinearLayout ll =(LinearLayout)tabHost.getChildAt(0); TabWidget tw =(TabWidget)ll.getChildAt(0); 並替換爲 TabWidget tw = tabHost.getTabWidget(); – Fred 2011-11-09 14:51:58

0

我做了這麼:

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