更改动作条的高度在Android软糖 [英] Change Actionbar height on Android JellyBean

查看:160
本文介绍了更改动作条的高度在Android软糖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在开发一个Android应用程序,其中我需要有一个自定义布局和尺寸的标签栏。我做了到现在为止的方法是使用杰克沃顿商学院的ActionBarSherlock库,支持pre-蜂巢的Andr​​oid版本,并通过应用一种风格中,我编辑actionBarSize风格项目的应用程序。

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 Wharton's ActionBarSherlock library to support pre-HoneyComb Android versions, and by applying a style to the app in which i edit the actionBarSize style item.

现在,我一直在测试在Galaxy S3(与杰利贝恩就可以了)的应用程序,并根据标签栏的高度不会再更改 actionBarSize 值。

Now, I've been testing the app on the Galaxy S3 (with Jellybean on it), and the tab bar height doesn't change anymore according to the actionBarSize value.

于是我开始寻找通过软糖的code,我发现这个方法在 ActionBarPolicy 类:

So I've started looking through JellyBean's code, and I found this method in the ActionBarPolicy class:

    public int getTabContainerHeight() {
            TypedArray a = mContext.obtainStyledAttributes(null, R.styleable.ActionBar,
               com.android.internal.R.attr.actionBarStyle, 0);
            int height = a.getLayoutDimension(R.styleable.ActionBar_height, 0);
            Resources r = mContext.getResources();
            if (!hasEmbeddedTabs()) {
                // Stacked tabs; limit the height
                height = Math.min(height,
                        r.getDimensionPixelSize(R.dimen.action_bar_stacked_max_height));
            }
            a.recycle();
            return height;
    }

这是我收集的这种方法,似乎软糖限制TabBar的高度时,当应用程序是在纵向模式下,通过设置标签栏高度的action_bar_stacked_max_height维度值(这是48dp,在4.1的 /values​​/dimen.xml 文件),即使我已经设置了操作栏高度 actionBarSize

From what i gather in this method, it seems that JellyBean limits the height of the TabBar, when the app is in portrait mode, by setting the tab bar height to the "action_bar_stacked_max_height" dimension value (which is 48dp, in 4.1's /values/dimen.xml file), even though I've set the action bar height in actionBarSize.

我试图重写这个尺寸值,将其设置为我自己的 dimen.xml 文件我自己的价值,但我有没有运气。它没有工作。

I've tried overriding this dimension value, by setting it to my own value in my own dimen.xml file, but i had no luck. It didn't work.

我的问题:

难道你们知道的一种方式,我可以覆盖action_bar_stacked_max_height扪价值呢?

Do you guys know of a way in which i can override the"action_bar_stacked_max_height" dimen value?

感谢你在前进!

推荐答案

在我看来,它做的目的,我不知道为什么。有在 bools.xml

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>

嵌入标签的高度是有限的在 ActionBarPolicy.java ,因为你已经提到。这就是为什么这样的行为可以看出,只有在Android的软糖或更高。我不能找到比做出一些Java反射更好的解决方案。因此,这里是code

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

使用方法 hackJBPolicy()的onCreate 。请注意,我用动作条从appcompat库。这里是链接与使用此解决办法的示例项目。

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.

毕竟,在我看来,这将是在未来更容易地创建对准屏幕,而不是在选项卡模式下使用动作条的顶部自定义视图,如果你的UI设计是一个有点远guidlines。

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.

这篇关于更改动作条的高度在Android软糖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆