更改PreferenceScreen时,使操作栏保持在显示状态 [英] Keep the actionbar displayed in when changing PreferenceScreen

查看:289
本文介绍了更改PreferenceScreen时,使操作栏保持在显示状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的首选项屏幕中显示一个操作栏. 为了做到这一点,我在我的SettingActivity中添加了以下代码

I'm trying to display a actionbar in my preference screen. In order to do so I added the following code in my SettingActivity

public class PreferencesActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferences_activity);
        getFragmentManager().beginTransaction()
                .replace(R.id.container, new PreferencesFragment()).commit();

            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

然后将我的其余代码放在PreferencesFragment中. 这可以正常工作,但是当我按下PreferenceScreen首选项时,操作栏将被隐藏.如果返回首选项主屏幕,我可以再次看到它. 知道如何保持操作栏显示(并使用PreferenceScreen标签更新)吗?

then the rest of my code in PreferencesFragment. This works fine, but as soon as I press on a PreferenceScreen preference, the actionbar is hidden. If I go back to the preference main screen I can see it again. Any idea how to keep the actionbar displayed (and updated with the PreferenceScreen label) ?

查看PreferenceScreen代码,当单击PreferenceScreen时,它看起来像一个全屏对话框.因为我的首选项有标题,所以对话框也应该显示标题...但是没有

Looking at the PreferenceScreen code it looks like a full screen Dialog is opened when the PreferenceScreen is clicked on. Because my preference has a title the Dialog should display a title as well... but it doesn't

    // Set the title bar if title is available, else no title bar
    final CharSequence title = getTitle();
    Dialog dialog = mDialog = new Dialog(context, context.getThemeResId());
    if (TextUtils.isEmpty(title)) {
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    } else {
        dialog.setTitle(title);
    }

推荐答案

我终于设法找到一种方法. 有点丑陋,但是可以用.

I finally managed to find a way to do this. It's kind of ugly but it works.

首先,我向我的preferences.xml文件中的每个PreferenceScreen定义添加相同的Intent(确保更新extra参数的值)

First I add an the same Intent to every PreferenceScreen definition in my preferences.xml file (make sure to update the value of the extra parameter)

        <PreferenceScreen
            android:key="pref1"
            android:summary="Summary1"
            android:title="Title1" >
            <intent
                android:action="android.intent.action.VIEW"
                android:targetPackage="my.package"
                android:targetClass="my.package.activity.PreferencesActivity" >
                <extra android:name="page" android:value="pref1" />
            </intent>
...

</PreferenceScreen>

顺便说一句my.package.activity.PreferencesActivity是我当前的偏好活动

BTW my.package.activity.PreferencesActivity is my current Preference Activity

然后我在清单中添加一个意图过滤器

Then I add an intent-filter in the Manifest

        <activity
            android:name=".activity.PreferencesActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/settings" >
            <intent-filter android:label="Pref" >
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.PREFERENCE" />
            </intent-filter>
  </activity>

我在PreferenceActivity中添加了一些代码来处理

I add some code in the PreferenceActivity to handle this

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.preferences_activity);

        this.fragment = new PreferencesFragment();
        this.fragment.setActivityIntent(getIntent());
        getFragmentManager().beginTransaction()
                .replace(R.id.container, this.fragment).commit();
    }

最后,我将以下代码添加到我的PreferencesFragment类中

Finally I add the following code in my PreferencesFragment class

    public void setActivityIntent(final Intent activityIntent) {
 if (activityIntent != null) {
                        if (Intent.ACTION_VIEW.equals(activityIntent.getAction())) {

                        if (intent.getExtras() != null) {
                            final String page = intent.getExtras().getString("page");
                            if (!TextUtils.isEmpty(page)) {
                                openPreferenceScreen(page);
                            }
                        }
                    }
    }

private void openPreferenceScreen(final String screenName) {
    final Preference pref = findPreference(screenName);
    if (pref instanceof PreferenceScreen) {
        final PreferenceScreen preferenceScreen = (PreferenceScreen) pref;
        ((PreferencesActivity) getActivity()).setTitle(preferenceScreen.getTitle());
        setPreferenceScreen((PreferenceScreen) pref);
    }
}

这篇关于更改PreferenceScreen时,使操作栏保持在显示状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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