tabStripEnabled用于旧版API中的TabWidget

时间:2010-07-02 08:53:22

标签: android

Android 2.2即API Level 8对于TabWidget具有tabStripEnabled =“true” 如何在旧版Android中实现相同的目标?

2 个答案:

答案 0 :(得分: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) {}
}

答案 1 :(得分: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();
    }