向 Theme.Black.NoTitleBar Android 添加操作栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11379916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding an action bar to Theme.Black.NoTitleBar Android
提问by EGHDK
I'm trying to add the action bar programatically as shown in the dev documentaionbut I'm coming across an error. My minSdk is set to 11, my application has one layout and one activity and the only code in the activity is:
我正在尝试以编程方式添加操作栏,如开发文档中所示,但我遇到了错误。我的 minSdk 设置为 11,我的应用程序有一个布局和一个活动,活动中唯一的代码是:
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox);
ActionBar actionBar = getActionBar();
actionBar.show();
}
If I take out those last two lines then my app runs. I know that the holo theme include the actionbar automatically, but I don't like the editText
views. Any ideas on why this is happening. Should I be using the HoloTheme
and themeing the views differently?
如果我取出最后两行,那么我的应用程序就会运行。我知道全息主题会自动包含操作栏,但我不喜欢这些editText
视图。关于为什么会发生这种情况的任何想法。我应该以HoloTheme
不同的方式使用和主题化视图吗?
Again, I'm not getting any errors in eclipse. My program is crashing from what I can decipher from logcat as an null pointer exception.
同样,我在 eclipse 中没有收到任何错误。我的程序崩溃了,我可以从 logcat 中将其解读为空指针异常。
回答by Darshan Rivka Whittle
From the documentation you linked to:
从您链接到的文档中:
If you have a custom activity theme in which you'd like to remove the action bar, set the
android:windowActionBar
style property tofalse
. However, if you remove the action bar using a theme, then the window will not allow the action bar at all, so you cannot add it later—callinggetActionBar()
will returnnull
.
如果您有要删除操作栏的自定义活动主题,请将
android:windowActionBar
style 属性设置为false
。但是,如果您使用主题删除操作栏,则窗口将根本不允许操作栏,因此您以后无法添加它——调用getActionBar()
将返回null
.
Since you're using a theme without an action bar, getACtionBar()
is returning null
, and then you're attempting to call show()
on that null
, resulting in an Exception being thrown.
由于您使用的是没有操作栏的主题,因此getACtionBar()
正在返回null
,然后您尝试调用show()
该主题null
,从而导致抛出异常。
So that explains the error you're getting. As far as what to do about it, the documentation for ActionBarsays:
这解释了你得到的错误。至于如何处理,ActionBar的文档说:
Beginning with Android 3.0 (API level 11), the action bar appears at the top of an activity's window when the activity uses the system's Holo theme (or one of its descendant themes), which is the default. You may otherwise add the action bar by calling
requestFeature(FEATURE_ACTION_BAR)
or by declaring it in a custom theme with thewindowActionBar
property.
从 Android 3.0(API 级别 11)开始,当活动使用系统的 Holo 主题(或其后代主题之一)时,操作栏会出现在活动窗口的顶部,这是默认设置。否则,您可以通过调用
requestFeature(FEATURE_ACTION_BAR)
或在自定义主题中使用windowActionBar
属性声明操作栏来添加操作栏。
That gives you two easy options to have an ActionBar without using a Holo theme. This is probably simplest in your case:
这为您提供了两个简单的选项,可以在不使用 Holo 主题的情况下使用 ActionBar。在您的情况下,这可能是最简单的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR); // Add this line
setContentView(R.layout.inbox);
ActionBar actionBar = getActionBar();
actionBar.show();
}
Update:You should also switch to just Theme.Black
without the NoTitleBar
part, as that prevents the ActionBar from working.
更新:您还应该切换到Theme.Black
不使用该NoTitleBar
部分,因为这会阻止 ActionBar 工作。
回答by Michael Stoner
I ran into the same problem. I eventually came up with a reasonable solution. I have a min api version 8 with a target api of 11. I don't like the holo styles either so I override the styles like the following:
我遇到了同样的问题。我最终想出了一个合理的解决方案。我有一个最小 api 版本 8,目标 api 为 11。我也不喜欢全息样式,所以我覆盖了如下样式:
First, you have to create a "res/values-v11" folder. You probably already have a "values" folder, just create a "values-v11" at the same level (doesn't matter what your target API level is...aways use "values-v11").
首先,您必须创建一个“res/values-v11”文件夹。您可能已经有一个“values”文件夹,只需在同一级别创建一个“values-v11”(不管您的目标 API 级别是什么......远离使用“values-v11”)。
Once that is created, you create an XML document containing your theme in each of the folders. The original values folder will be used for anything less than android 3.0 (< api 11). Your theme would look something like this.
创建后,您将在每个文件夹中创建一个包含您的主题的 XML 文档。原始值文件夹将用于低于 android 3.0 (< api 11) 的任何内容。你的主题看起来像这样。
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.Black.OptionalActionBar" parent="@android:style/Theme.Black.NoTitleBar">
<item name="android:windowNoTitle">True</item>
<item name="android:windowActionBar">False</item>
</style>
<style name="OptionalVisibility">
<item name="android:visibility">visible</item>
</style>
</resources>
Notice that I am NOT using the Holotheme but instead using the Theme.Black.NoTitleBartheme as the parent. From there, I set the options to hide the window title and action bar for things lower than api 11.
请注意,我没有使用Holo主题,而是使用Theme.Black.NoTitleBar主题作为父主题。从那里,我设置选项以隐藏低于 api 11 的内容的窗口标题和操作栏。
Then, I create a second theme document in the "values-v11" folder and set it up like this:
然后,我在“values-v11”文件夹中创建了第二个主题文档,并将其设置如下:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.Black.OptionalActionBar" parent="@android:style/Theme.Black.NoTitleBar">
<item name="android:windowNoTitle">false</item>
<item name="android:windowActionBar">true</item>
<item name="android:actionBarStyle">@android:style/Widget.Holo.ActionBar</item>
</style>
<style name="OptionalVisibility">
<item name="android:visibility">invisible</item>
<item name="android:height">1dp</item>
</style>
</resources>
In the above theme, it enablesthe action bar and sets it up with the Holotheme, but leaves all the other elements alone. So the action bar looks "holo"-ish but everything else stays exactly as it appears in the lower versions (and I don't get the ugly text entry that comes with Holo).
在上面的主题中,它启用了操作栏并将其设置为Holo主题,但保留所有其他元素。所以操作栏看起来像“全息”,但其他所有内容都与它在较低版本中显示的完全一样(而且我没有得到 Holo 附带的难看的文本条目)。
Then in the manifest if I want to have the actionbar show up, I just set the theme for that activity to android:theme="@style/Theme.Black.OptionalActionBar"
然后在清单中,如果我想显示操作栏,我只需将该活动的主题设置为 android:theme="@style/Theme.Black.OptionalActionBar"
I did this specifically because I failed on my layouts by relying on the, now deprecated, hardware menu button. With the use of the themes above, I can setup the action bar if it is required or leave it as menus if it is an older device. Then, all I have to do is set a single property on my menu creation methods to support 2.0 through 4.0 seamlessly.
我之所以这样做,是因为我依赖于现在已弃用的硬件菜单按钮在布局上失败了。通过使用上述主题,如果需要,我可以设置操作栏,或者如果它是较旧的设备,则将其保留为菜单。然后,我所要做的就是在我的菜单创建方法上设置一个属性,以无缝支持 2.0 到 4.0。
At any rate, take a look and see if this gets you to where you need to be.
无论如何,看看这是否能让你到达你需要的地方。
In my mind it is a lot easier than the other approaches because you don't have to remember to request the actionbar or do anything in code. You just set your theme on your activity...and off you go.
在我看来,它比其他方法容易得多,因为您不必记住请求操作栏或在代码中执行任何操作。您只需为您的活动设置主题……然后就可以了。
回答by Idistic
What Darshan Said ... kind of
Darshan 所说的......有点
To go a little further I would check out ActionBarSherlock, in combination with the support library you can then add the action bar to pretty much any version of android that is 2.x+
为了更进一步,我会查看 ActionBarSherlock,结合支持库,您可以将操作栏添加到几乎任何 2.x+ 版本的 android
To get a full screen / no title bar you would create your own theme from one of the ABS theme's similar to
要获得全屏/无标题栏,您可以从类似于的 ABS 主题之一创建自己的主题
<style name="Theme.MY.Sherlock.Fullscreen" parent="Theme.Sherlock.Light.DarkActionBar">
<item name="android:windowFullscreen">true</item>;
</style>
Applying that theme in your onCreate and then getting the action bar to show is the same (except you would use the ABS support prefix ) so basically
在您的 onCreate 中应用该主题然后让操作栏显示是相同的(除非您将使用 ABS 支持前缀)所以基本上
setTheme(R.style.Theme_MY_Sherlock_Fullscreen);
setContentView(R.layout.mylayout);
getSupportActionBar.Show();
I am assuming you can do the same for a standard action bar (using the holo theme as a base) since ABS is pretty much a duplicate functionality wise (and much of the source, ABS is mostly a wrapper class) it even detects and then uses the native implementation on 14 and above (11 and in-between has some missing features / fixes )
我假设您可以对标准操作栏执行相同的操作(使用全息主题作为基础),因为 ABS 几乎是一个重复的功能(并且大部分源代码,ABS 主要是一个包装类)它甚至可以检测然后在 14 及更高版本上使用本机实现(11 及之间有一些缺失的功能/修复)
You can take a look at ABS hereand it looks like he just updated it to JB
你可以看看这里的ABS,看起来他刚刚更新到JB
-- In your resources folder under values create a themes.xml file ( /res/values/themes.xml )
-- 在您的资源文件夹下的 values 创建一个 themes.xml 文件( /res/values/themes.xml )
Then put this in there and give the above a try, but of course in setTheme( replace the name with Theme_My_Holo_FullScreen and dont use the getSupportActionbar(). prefix, I never bother using the native implementation but it should work the same.
然后把它放在那里并尝试上面的内容,但当然在 setTheme( 用 Theme_My_Holo_FullScreen 替换名称并且不要使用 getSupportActionbar() 前缀,我从不打扰使用本机实现,但它应该工作相同。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.My.Holo.Fullscreen" parent="Theme.Holo">
<item name="android:windowFullscreen">true</item>;
</style>
</resources>