Android:如何通过xml中的操作栏从首选项子屏幕导航回来?

时间:2013-07-14 13:02:33

标签: android xml navigation android-actionbar preferencescreen

我使用SettingsActivity进行现代PreferenceFragement。我完全用XML定义了它的布局。在我的设置中,我有一个偏好子屏幕。点击它即可显示新的PreferenceScreen

看起来我只能使用后退按钮导航回主设置屏幕。有没有办法通过子屏幕中的ActionBar启用导航?我更喜欢通过XML启用它。

提前致谢, kaolick

修改

我想在右侧像左侧一样:

enter image description here

以下是Google的相应代码段:

<PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- opens a subscreen of settings -->
    <PreferenceScreen
        android:key="button_voicemail_category_key"
        android:title="@string/voicemail"
        android:persistent="false">
        <ListPreference
            android:key="button_voicemail_provider_key"
            android:title="@string/voicemail_provider" ... />
        <!-- opens another nested subscreen -->
        <PreferenceScreen
            android:key="button_voicemail_setting_key"
            android:title="@string/voicemail_settings"
            android:persistent="false">
            ...
        </PreferenceScreen>
        <RingtonePreference
            android:key="button_voicemail_ringtone_key"
            android:title="@string/voicemail_ringtone_title"
            android:ringtoneType="notification" ... />
        ...
    </PreferenceScreen>
    ...
</PreferenceScreen>

1 个答案:

答案 0 :(得分:1)

首先在getActionBar().setDisplayHomeAsUpEnabled(true);中添加onCreate()

然后尝试:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(android.R.id.home ==  item.getItemId() ){
        //  try one of these:

        // dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));

        // getFragmentManager().popBackStack();

        // finish();

        return true;
    }

    return super.onOptionsItemSelected(item);

}