23

我最近开发了一个Android应用程序,其中我需要为选项卡栏提供自定义布局和尺寸。我到现在为止的做法是通过使用Jake Wharton的ActionBarSherlock库来支持HoneyComb之前的Android版本,并通过将样式应用到我编辑actionBarSize样式项目的应用程序中。更改Android上的Actionbar高度JellyBean

现在,我已经测试了Galaxy S3上的应用程序(上面有Jellybean),并且根据actionBarSize值,标签栏高度不再改变。

所以我已经开始期待通过豆形软糖的代码,我发现在ActionBarPolicy类此方法:

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的高度,当应用处于纵向模式时,即使我已将动作栏高度设置为actionBarSize,通过将标签栏高度设置为“action_bar_stacked_max_height”尺寸值(即4.1d的012d文件中为48dp)。

我试过重写这个维度值,通过将其设置为我自己的值在我自己的dimen.xml文件中,但我没有运气。它没有工作。

我的问题:

难道你们知道的方式,我可以覆盖“action_bar_stacked_max_height”扪价值?

预先感谢您!

+0

我也有同样的问题。你有没有找到解决方案? – user936414 2013-09-15 02:48:36

回答

1

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

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

嵌入凸片的高度是有限的,ActionBarPolicy.java到48dip,因为你已经提到。这就是为什么只有在Android JellyBean或更高版本中才能看到这种行为。我找不到一个更好的解决方案,而不是做一些Java反射。因此,这里的代码

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库中使用了ActionBar。以下是使用此解决方法的示例项目的link

毕竟,在我看来,如果你的UI设计离guidlines有点远,那么在将来可以更容易地创建与屏幕顶部对齐的自定义视图,而不是在标签模式下使用ActionBar。

+0

您能否请解释如何使用标准操作栏来实现该功能,而不是从支持图书馆?我找不到名为R.id.action_bar_container的资源。 – user2302510 2014-09-17 12:56:07

+0

我知道这是一个非常晚的评论,但我会接受st_的答案,即使它使用反射(理论上会减慢应用程序的一点)。但是他提供了足够的信息,以便人们通过“JB政策”。最后,我开发了自己的自定义标签栏,正如st_所说的。 – edy 2014-10-15 08:48:56

26

尝试把android:actionBarSizeactionBarSize为主题,你正在使用,就像这样:

<style name="Theme.white_style" parent="@android:style/Theme.Holo.Light.DarkActionBar"> 
     <item name="android:actionBarSize">55dp</item> 
     <item name="actionBarSize">55dp</item> 
</style> 
+6

这不适用于果冻豆。 – Tomik 2013-03-01 00:05:55

+1

这对Jellybean有效。我已在Nexus 4 – Anton 2013-03-01 20:46:35

+4

上确认它,它改变了主要操作栏的高度,但它不会影响标签栏的高度(绝对不是Android 4.2.2)。 – Tomik 2013-03-01 23:09:50

9

尝试使用android:height类似如下:

<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> 
+1

适用于拆分操作栏但不适用于第二行/堆叠条(标签) – jfcartier 2013-12-16 19:23:36