动作条在preferenceActivity [英] ActionBar in PreferenceActivity
问题描述
在我的应用程序正在使用从谷歌的新操作栏的兼容性样本(位于< SDK> /样本/ android-<版> / ActionBarCompat
)的伟大工程。唯一的问题我正在申请这对我的 preferenceActivity
为了得到像在Android Market中的设置画面(见图片)。
In my application I am using the new Action Bar Compatibility sample from Google (located at <sdk>/samples/android-<version>/ActionBarCompat
) which works great. The only problem I have is applying this to my PreferenceActivity
in order to get a screen like the settings in the Android Market (see picture).
要填写动作条
的图标,每个活动
必须扩展 ActionBarActivity
类。问题是,我的活动
已扩展 preferenceActivity
和Java类不能扩展多个类。
To fill the ActionBar
with icons, each Activity
must extend the ActionBarActivity
class. The problem is that my Activity
already extends PreferenceActivity
and in Java classes can not extend more than one class.
必须有一种方式来获得动作条
连同 preferenceScreen
。我会很高兴,如果有人可以为这个共同的问题的解决方案。
There must be a way to get the ActionBar
together with a PreferenceScreen
. I would be glad if anybody could provide a solution for this common issue.
PS:像如何添加一个按钮, preferenceScreen 不适合,因为动作条
实际上是标题栏,所以这更是一个比Java布局的事情。
P.S.: A solution like in How to add a button to PreferenceScreen does not fit because the ActionBar
is actually the title bar and so this is more a Java than a layout thing.
推荐答案
编辑:下面我的答案是相当哈克,它好像是已经过时(为pre的Android 3.0)有看看其他的答案少哈克多的现有解决方案的〜PYKO 2014年9月1日的
My answer below is rather hacky and it seems like it is now outdated (for pre Android 3.0) Have a look at the other answers for less hacky and more current solutions ~pyko 2014-09-01
我设法得到它的工作 - 不知道这是最好的/干净的解决方案,但它的工作原理
I managed to get it working - not sure if this is the nicest/cleanest solution, but it works.
不得不做出如下更改:
-
请
ActionBarActivity
的副本,并有新的类扩展preferenceActivity
Make a copy of
ActionBarActivity
and have the new class extendPreferenceActivity
public abstract class ActionBarPreferenceActivity extends PreferenceActivity {
// contents exactly the same as 'ActionBarActivity'
}
修改的onCreate()
在 ActionBarHelperBase.java
略 - 做一个特殊的案例 preferenceActivity
类
Modify onCreate()
in ActionBarHelperBase.java
slightly - make a special case for PreferenceActivity
classes
@Override
public void onCreate(Bundle savedInstanceState) {
// If the activity is a PreferenceActivity, don't make the request
if (!(mActivity instanceof PreferenceActivity)) {
mActivity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
}
让您的preferenceActivity扩展这个类并添加请求 FEATURE_CUSTOM_TITLE
你叫超之前.onCreate()
Have your PreferenceActivity extend this class and add request for FEATURE_CUSTOM_TITLE
before you call super.onCreate()
public class MyPreferenceActivity extends ActionBarPreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // add this line
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// etc etc
}
// etc etc
}
据我所知,改变了2和3,因为需要 preferenceActivity
:
As far as I can tell, changes 2 and 3 are needed because for PreferenceActivity
:
当你调用super.onCreate(),该ViewGroup中会成立,因此,你不能改变窗口的参数。 (<一href="http://stackoverflow.com/questions/4813831/requestwindowfeaturewindow-feature-no-title-gives-the-exception">see奥利弗的评论答案)的
"As soon as you call super.onCreate(), the ViewGroup will be set up and so, you are not allowed to change the Window's parameters." (see Oliver's comment to the answer)
我想怎么组件的顺序 preferenceActivity
活动创建不同于普通的活动
活动。
I guess the order of how components in PreferenceActivity
activities are created is different to plain Activity
activities .
这篇关于动作条在preferenceActivity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!