动作条容量/溢出而不是方向变化而变化 [英] ActionBar capacity/overflow not changing on orientation change

查看:155
本文介绍了动作条容量/溢出而不是方向变化而变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用动作条,在那里我处理方向更改自己的应用程序:

 安卓configChanges =键盘| keyboardHidden |方向|屏幕尺寸
 

...和菜单应适合在动作条无溢出的风景,而不是在纵向:

 < XML版本=1.0编码=UTF-8&GT?;
<菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<项目的android:标题=@字符串/游戏机器人:ID =@ + ID /游戏机器人:图标=@机器人:可绘制/ ic_menu_manage机器人:showAsAction =ifRoom | withText/>
<项目的android:标题=@字符串/类型机器人:ID =@ + ID /型机器人:图标=@机器人:可绘制/ ic_menu_edit机器人:showAsAction =ifRoom | withText/>
<项目的android:标题=@字符串/其他机器人:ID =@ + ID /其他机器人:图标=@机器人:可绘制/ ic_menu_gallery机器人:showAsAction =ifRoom | withText/>
<项目的android:标题=@字符串/解决机器人:ID =@ + ID /解决机器人:图标=@机器人:可绘制/ ic_menu_directions机器人:showAsAction =ifRoom | withText/>
<项目的android:标题=@字符串/帮助机器人:ID =@ + ID /帮助机器人:图标=@机器人:可绘制/ ic_menu_help机器人:showAsAction =ifRoom/>
< /菜单>
 

在启动时,该工作正常:

景观:

人像: (是的,我可能会迫使所有项目始终显示,他们会适合,如下图所示,但可能在更小的平板电脑打破)

当模拟器改变方向,ActionBar的能力似乎并没有改变:

人像,当我开始在景观: (这是确定的,但不一致的)

风景,当我开始在纵向: 这看起来真的很傻,是我想解决这个问题的原因。

我添加了这个呼叫<一个href="http://developer.android.com/reference/android/app/Activity.html#invalidateOptionsMenu()">invalidateOptionsMenu(),但它并不能帮助:

  @覆盖
公共无效onConfigurationChanged(配置NEWCONFIG)
{
    maybeMoveSomeViewsAround(NEWCONFIG);
    super.onConfigurationChanged(NEWCONFIG);
    invalidateOptionsMenu();
}
 

(其实我叫它反射的向后兼容性,但调试器告诉我,这真的是所谓的,不遇到异常。)

invalidateOptionsMenu()实际上结束了在返回之前调用onCreateOptionsMenu()(它重新膨胀菜单),我可以在里面后者是getResources()。getConfiguration()。方向已经改变了看。因此,这实在令人费解。如果选项菜单被重新创建,当方向发生了变化,它必须是动作条本身缓存的宽度?

有没有办法来重新创建不破坏/创建活动的动作条?(因为后者是在我的情况有点贵)

编辑:下面是一个显示问题href="http://chris.boyle.name/tmp/20111002-ActionBarTest.tgz">最小的样本项目<。 / P>

编辑2:我原以为检查屏幕宽度和编程调整之间始终和永远正确的showAsAction标志,但是这需要知道(或猜测),每个项目的宽度。 ActionBar的公共API 并不能帮助我在这一点上。

解决方案

我一直小心翼翼地工作围绕这一点:当设备的宽度大于850dip,力表示在动作条的所有项目,否则继续让平台的决定。

这里的git的承诺修改后续提交使用修复现场这是太新了,哎呀呀。 : - )

我肯定还有兴趣更好的答案(而不是等待修复的平台)。

I have an app using the ActionBar, where I handle orientation changes myself:

android:configChanges="keyboard|keyboardHidden|orientation|screenSize"

...and the menu should fit in the ActionBar without overflow in landscape, but not in portrait:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="@string/Game"  android:id="@+id/game"  android:icon="@android:drawable/ic_menu_manage"     android:showAsAction="ifRoom|withText"/>
<item android:title="@string/Type"  android:id="@+id/type"  android:icon="@android:drawable/ic_menu_edit"       android:showAsAction="ifRoom|withText"/>
<item android:title="@string/Other" android:id="@+id/other" android:icon="@android:drawable/ic_menu_gallery"    android:showAsAction="ifRoom|withText"/>
<item android:title="@string/Solve" android:id="@+id/solve" android:icon="@android:drawable/ic_menu_directions" android:showAsAction="ifRoom|withText"/>
<item android:title="@string/Help"  android:id="@+id/help"  android:icon="@android:drawable/ic_menu_help"       android:showAsAction="ifRoom"/>
</menu>

On startup, this works correctly:

Landscape:

Portrait: (yes, I could force all items to always display and they would fit, as shown below, but that might break on a smaller tablet)

When the emulator changes orientation, the ActionBar's capacity doesn't seem to change:

Portrait, when I started in landscape: (this is ok, but inconsistent)

Landscape, when I started in portrait: This looks really silly and is the reason I want to fix this.

I added this call to invalidateOptionsMenu(), but it doesn't help:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    maybeMoveSomeViewsAround(newConfig);
    super.onConfigurationChanged(newConfig);
    invalidateOptionsMenu();
}

(Actually I call it by reflection for backward compatibility, but the debugger tells me it really is called and does not encounter an exception.)

invalidateOptionsMenu() actually ends up calling onCreateOptionsMenu() (which re-inflates the menu) before returning, and I can see inside the latter that getResources().getConfiguration().orientation has already changed. So this is really puzzling. If the options menu is being recreated, when the orientation has changed, it must be ActionBar itself caching the width?

Is there a way to re-create the ActionBar without destroying/creating the Activity? (because the latter is a bit expensive in my case)

Edit: Here's a minimal sample project showing the issue.

Edit 2: I had thought of checking the screen width and programmatically adjusting the showAsAction flags between always and never appropriately, but that requires knowing (or guessing) the width of each item. ActionBar's public API does not help me on that point.

解决方案

I've cautiously worked around this: when the device's width is greater than 850dip, force showing all items in the ActionBar, otherwise continue to let the platform decide.

Here's the git commit. Edit: and the follow-up commit to fix using a field that's too new, oops. :-)

I'm definitely still interested in better answers (other than waiting for a fix to the platform).

这篇关于动作条容量/溢出而不是方向变化而变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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