如果标题栏不是 ActionBar,则隐藏标题栏 - 在 setContentView 之前预测 ActionBar 是否存在?
如果我的全屏首选项(默认情况下关闭)打开,我希望我的活动隐藏标题栏和通知栏。我可以用 requestWindowFeature(Window.FEATURE_NO_TITLE);
+ FLAG_FULLSCREEN
等或者 setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
来做到这一点
其中任何一个都必须在 setContentView()
之前发生,否则将会崩溃。困难的部分是:
- 我的目标是 API 级别 3 到 14,并且我只想在 Activity 不打算使用 ActionBar 时隐藏标题栏。否则,
FEATURE_NO_TITLE
将隐藏 ActionBar 并丢失一些重要的控件。 (我会选择“熄灯”模式。) - 我不知道如何确定是否会使用 ActionBar,直到之后
setContentView()
,当为时已晚。
不起作用的事情:
getActionBar()
在早期阶段返回 null。getWindow.hasFeature(Window.FEATURE_ACTION_BAR)
为 false。- 知道(通过反射)ActionBar 作为类存在是不够的,因为坚持使用默认设备主题,可能并不总是在某些 Ice Cream Sandwich 设备上使用 ActionBar,即使平台有它。 (?)
- 或者:我可以依赖 ICS 始终拥有 ActionBar 吗?为什么?
- 编辑:好吧,也许 ICS 的标题栏仍然是一个 ActionBar,只是没有任何项目,甚至没有溢出按钮? (在这种情况下我仍然想隐藏它。)任何人都可以阐明那里吗?
有什么想法吗? :-)
If my fullscreen preference (off by default) is on, I want my Activity to hide the title bar as well as the notification bar. I can do that with requestWindowFeature(Window.FEATURE_NO_TITLE);
+ FLAG_FULLSCREEN
etc. or perhaps setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
Either of these must happen before setContentView()
or they will crash. The hard part is:
- I target API levels 3 to 14, and I only want to hide the title bar if the Activity is not going to use an ActionBar. Otherwise,
FEATURE_NO_TITLE
will hide the ActionBar and lose some important controls. (I'll instead settle for "lights-out" mode.) - I can't see how to find out whether an ActionBar will be used until after
setContentView()
, when it's too late.
Things that don't work:
getActionBar()
returns null at this early stage.getWindow.hasFeature(Window.FEATURE_ACTION_BAR)
is false.- Knowing (by reflection) that ActionBar exists as a class is insufficient, because, sticking with the default device theme, that might not always use ActionBar on some Ice Cream Sandwich devices, even though the platform has it. (?)
- Alternatively: can I rely on ICS always having an ActionBar? Why?
- Edit: ok, maybe ICS' title bar is still an ActionBar, just with no items and not even the overflow button? (In which case I still want to hide it.) Can anyone shed any light there?
Any ideas? :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您想要使用的是 ActionBarSherlock。它会为您处理所有反射和 API 级别检测。
I think what you want to use instead is ActionBarSherlock. It takes care of all the reflection and API level detection for you.
是否可以执行此处和这里?基本上指定在某些 api 级别上隐藏标题栏(因为您将拥有操作栏)。
Would it be possible to do something like suggested here and here? Basically specify that on certain api levels, you hide the title bar (because you will have the action bar).