Force stacked tabs

后端 未结 4 837
暗喜
暗喜 2020-12-17 03:07

Is there any way to force stacked tabs? I want tabs separate of action bar (in a second row), even when landscape mode.

I am trying to force it but I can´t. For exam

4条回答
  •  庸人自扰
    2020-12-17 03:19

    I had luck using the following reflection 'hack':

    private void forceStackedTabs() {
        ActionBar ab = getSupportActionBar();
        if ( ab instanceof ActionBarImpl ) {
            // Pre-ICS
            disableEmbeddedTabs( ab );
        } else if ( ab instanceof ActionBarWrapper ) {
            // ICS
            try {
                Field abField = ab.getClass().getDeclaredField( "mActionBar" );
                abField.setAccessible( true );
                disableEmbeddedTabs( abField.get( ab ) );
            } catch (NoSuchFieldException e) {
                Log.e( TAG, "Error disabling actionbar embedded", e );
            } catch (IllegalArgumentException e) {
                Log.e( TAG, "Error disabling actionbar embedded", e );
            } catch (IllegalAccessException e) {
                Log.e( TAG, "Error disabling actionbar embedded", e );
            }
        }
    }
    private void disableEmbeddedTabs(Object ab) {
        try {
            Method setHasEmbeddedTabsMethod = ab.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
            setHasEmbeddedTabsMethod.setAccessible(true);
            setHasEmbeddedTabsMethod.invoke(ab, false);
        } catch (Exception e) {
            Log.e( TAG, "Error disabling actionbar embedded", e );
        }
    }
    

    Please note that I didn't think of this myself, but simply rewrote the code given in this answer: replicate ActionBar Tab(s) with custom view

提交回复
热议问题