代码之家  ›  专栏  ›  技术社区  ›  amithgc

为旧API中的TabWidget启用tabStripEnabled

  •  8
  • amithgc  · 技术社区  · 14 年前

    如何在旧版本的Android中实现同样的功能?

    2 回复  |  直到 14 年前
        1
  •  8
  •   Freewind thk    12 年前
    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) {}
    }
    
        2
  •  0
  •   Ilya Lysenko    13 年前

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