为什么我的 Android ActionBar 在显式更改时不更新 [英] Why my Android ActionBar doesn't update when it is explictily changed

本文介绍了为什么我的 Android ActionBar 在显式更改时不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经多次看到有关 SO 的问题.所以我决定发布它发生的原因以及解决方案,以便许多 SO 用户将从答案中受益.

I've seen questions on SO that related to these many times. So I've decided to post the reason why it happens and also the solution so that many SO users will get the benefit of the answer.

现在有一段时间,我看到一个反复出现的问题,我会更改 ActionBar 标题,但它会被椭圆化得比它应该的要短.这种情况的另一个症状是在不应该或完全保持空白时让您的标题换行.当您更改操作栏按钮时,它不会正确更新,并且我将没有操作栏按钮,即使它们确实已添加或在现有的操作栏按钮上加倍.

For sometime now I've seen a reoccurring issue where I would change the ActionBar title and it would get ellipsized shorter than it should. Another symptom of this is having your title wrap when it shouldn't or just stay completely blank. When you change the the action bar buttons it wouldn't update correctly and I would be left with either no action bar buttons even though they have definitely been added or double up on existing action bar buttons.

事实证明这是一个相对简单的问题.它主要发生在您使用 ViewPagergetChildFragmentManager() 来管理片段时.

It turns' out this is a relatively straight forward issue. It mainly happens when you use a ViewPager with the getChildFragmentManager() to manage your fragments.

推荐答案

承载 ViewPager 的片段和由您的适配器创建的子 Fragment 也必须在它们的 onCreate() 方法中调用 setHasOptionsMenu(true);,这样所有的选项菜单事件都会传递给子片段,然后传递给它们的子片段,依此类推.

The fragment that hosts the ViewPager and the child Fragments that are created by your adapter must also call setHasOptionsMenu(true); in their onCreate() method's, so that all of the option menu events get passed down to the child fragments and on to their children and so on.

想想就很简单.但这只是其中之一.

Simple really, when you think about it. But its just one of those things.

希望这会有所帮助,欢迎您提出意见,西蒙

Hope this helps and I welcome your comments, Simon

这篇关于为什么我的 Android ActionBar 在显式更改时不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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