开发者

tabStripEnabled for TabWidget in Older API's

Android 2.2 i.e API Level 8 has tabStripEnabled="true" for T开发者_JAVA百科abWidget how to achieve the same in Older versions of Android?


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) {}
}


I made it so:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜