设置操作栏标题和副标题 [英] Setting Action Bar title and subtitle
问题描述
我想之前设置标题我的行动吧,并subtitile编译时间。 我有一个办法做到这一点是这样的:
I want to set title and subtitile of my action bar before compile time. I got a way to do it like this:
ActionBar ab = getActionBar();
ab.setTitle("My Title");
ab.setSubtitle("sub-title");
但我并不想这样做的运行时间。是否有任何XML文件或在那里我可以指定这些标题的任何位置?
But I don't want to do it on run time. Is there any xml file or any location where I can specify these titles?
我试图做到这一点:
为什么我想在XML的原因是,我希望我的应用程序,以在API级别8的支持,并且该方法getActionBar()支持至少在11的水平。
The reason why I want it in xml is that I want my app to be supported in API level 8. And the method getActionBar() is supported at least on level 11.
推荐答案
您可以做这样的事情,以code两个版本:
You can do something like this to code for both versions:
/**
* Sets the Action Bar for new Android versions.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void actionBarSetup() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ActionBar ab = getActionBar();
ab.setTitle("My Title");
ab.setSubtitle("sub-title");
}
}
然后调用 actionBarSetup()
在的onCreate()
。该如果
仅运行在新的Android版本和 @TargetApi
的code允许code到编译。因此,它使安全的旧的和新的API版本。
Then call actionBarSetup()
in onCreate()
. The if
runs the code only on new Android versions and the @TargetApi
allows the code to compile. Therefore it makes it safe for both old and new API versions.
另外,您还可以使用 ActionBarSherlock (看到修改的),所以你可以有动作条上的所有版本。你将不得不做一些变化,如让你的活动延长 SherlockActivity
并调用 getSupportActionBar()
,但是,它是一个很好的全球性解决方案。
Alternatively, you can also use ActionBarSherlock (see edit) so you can have the ActionBar on all versions. You will have to do some changes such as making your Activities extend SherlockActivity
and calling getSupportActionBar()
, however, it is a very good global solution.
请注意,当这个答案最初写, ActionBarSherlock
,这之后一直的德precated 的,是去到兼容性解决方案。
Note that when this answer was originally written, ActionBarSherlock
, which has since been deprecated, was the go-to compatibility solution.
目前,谷歌的 appcompat-V7
库提供相同的功能,但支持(积极更新)由谷歌。活动希望实施<一个href="https://developer.android.com/reference/android/support/v7/app/ActionBar.html"><$c$c>ActionBar$c$c>必须:
Nowadays, Google's appcompat-v7
library provides the same functionality but is supported (and actively updated) by Google. Activities wanting to implement an ActionBar
must:
- 延长
AppCompatActivity
- 使用
Theme.AppCompat
衍生
- extend
AppCompatActivity
- use a
Theme.AppCompat
derivative
要使用这个库获得动作条
举例来说,恰当地命名为 getSupportActionBar()
法。
To get an ActionBar
instance using this library, the aptly-named getSupportActionBar()
method is used.
这篇关于设置操作栏标题和副标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!