Change Actionbar height on Android JellyBean

后端 未结 3 1034
离开以前
离开以前 2020-12-01 07:49

I\'ve been recently developing an Android app, in which i need to have a custom layout and dimension for the tab bar. The way that i did it until now is by using Jake Wharto

相关标签:
3条回答
  • 2020-12-01 08:36

    Try putting android:actionBarSize and actionBarSize under the Theme you are using, like so:

    <style name="Theme.white_style" parent="@android:style/Theme.Holo.Light.DarkActionBar">
            <item name="android:actionBarSize">55dp</item>
            <item name="actionBarSize">55dp</item>
    </style> 
    
    0 讨论(0)
  • 2020-12-01 08:48

    it seems to me that it was done on purpose and I don't know why. There are some variables in bools.xml

    <bool name="action_bar_embed_tabs">true</bool>
    <bool name="action_bar_embed_tabs_pre_jb">false</bool>
    

    The height of embed tabs is limited to 48dip in ActionBarPolicy.java, as you have already mentioned. That's why such behaviour can be seen only in Android JellyBean or higher. I can't find a better solution than to make some java reflection. So here is the code

    private void hackJBPolicy() {
        View container = findScrollingTabContainer();
        if (container == null) return;
    
        try {
            int height = getResources().getDimensionPixelSize(R.dimen.action_bar_height);
            Method method = container.getClass()
                    .getDeclaredMethod("setContentHeight", Integer.TYPE);
            method.invoke(container, height);
        } catch (NoSuchMethodException e) {
            Log.e(LOG_TAG, e.getLocalizedMessage(), e);
        } catch (IllegalArgumentException e) {
            Log.e(LOG_TAG, e.getLocalizedMessage(), e);
        } catch (IllegalAccessException e) {
            Log.e(LOG_TAG, e.getLocalizedMessage(), e);
        } catch (InvocationTargetException e) {
            Log.e(LOG_TAG, e.getLocalizedMessage(), e);
        }
    }
    
    private View findScrollingTabContainer() {
        View decor = getWindow().getDecorView();
        int containerId = getResources().getIdentifier("action_bar_container", "id", "android");
    
        // check if appcompat library is used
        if (containerId == 0) {
            containerId = R.id.action_bar_container;
        }
    
        FrameLayout container = (FrameLayout) decor.findViewById(containerId);
    
        for (int i = 0; i < container.getChildCount(); i++) {
            View scrolling = container.getChildAt(container.getChildCount() - 1);
            String simpleName = scrolling.getClass().getSimpleName(); 
            if (simpleName.equals("ScrollingTabContainerView")) return scrolling;
        }
    
        return null;
    }
    

    Use the method hackJBPolicy() in your onCreate. Notice that I used ActionBar from appcompat library. Here is the link to the sample project with the use of this workaround.

    After all, it seems to me that it would be easier in future to create custom view aligned to the top of the screen instead of using ActionBar in tabs mode if your ui design is a bit far from guidlines.

    0 讨论(0)
  • 2020-12-01 08:51

    Try using android:height like the following:

    <style name="AppActionBar">
        <item name="android:height">50dp</item>
    </style>
    
    <style name="MainActivityStyle" parent="@android:style/Theme.Holo">
        <item name="android:actionBarStyle">@style/AppActionBar</item>
    </style>
    
    0 讨论(0)
提交回复
热议问题