2010-07-02 20 views
8

Android 2.2 tức là API Cấp 8 có tabStripEnabled = "true" cho TabWidget cách đạt được điều tương tự trong phiên bản cũ hơn của Android?tabStripEnabled cho TabWidget trong API cũ hơn của

+0

tôi đã Tabs ở dưới cùng của màn hình, Vì vậy, tôi đã làm điều đó như sau ... tôi đặt android: layout_marginBottom = "- 10dip" di chuyển bottomStrip khỏi màn hình nhưng muốn biết một cách chính xác để làm điều đó ... nhờ – amithgc

Trả lời

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

Cảm ơn nhiều!!!! – Eby

+0

Điều này không hiệu quả đối với tôi. Tôi đã thử cả hai trên một giả lập 2.1 và 2.2. Có điều gì khác mà tôi nên xem xét khi sử dụng hack này? Mã được thực thi đúng dựa trên SDK hiện tại nhưng đường viền dưới cùng cho TabWidget vẫn được giữ nguyên. – dannyroa

+1

Điều này làm việc hoàn hảo, một số điều cần lưu ý, tạo ra một hình ảnh minh bạch và đặt tên là trống. Tôi đã thực hiện một thay đổi nhỏ bằng cách nhận xét: LinearLayout ll = (LinearLayout) tabHost.getChildAt (0); TabWidget tw = (TabWidget) ll.getChildAt (0); và thay thế bằng TabWidget tw = tabHost.getTabWidget(); – Fred

0

tôi đã làm cho nó như vậy:

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(); 
    } 
Các vấn đề liên quan