操作栏主页按钮在嵌套首选项屏幕下不起作用

我找到了一个解决方法,可以实际启用嵌套的首选项屏幕上的ActionBar主页按钮...但是,它不会在我的首选项活动中调用 OnOptionsItemSelected。有谁知道在嵌套的首选项屏幕上实际使用主页按钮的方法吗?

修改35号帖子:

http://code.google.com/p/android/issues/detail?id=4611

@Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)
    {
        super.onPreferenceTreeClick(preferenceScreen, preference);
        if (preference!=null)
            if (preference instanceof PreferenceScreen)
                if (((PreferenceScreen)preference).getDialog()!=null)
                    ((PreferenceScreen)preference).getDialog().getActionBar().setHomeButtonEnabled(true);
        return false;
    }

答案 1

我最近遇到了这个问题,这就是我解决它的方式。首先,要访问首选项屏幕,我使用与您上面提到的完全相同的方法。

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    super.onPreferenceTreeClick(preferenceScreen, preference);

    // If the user has clicked on a preference screen, set up the action bar
    if (preference instanceof PreferenceScreen) {
        initializeActionBar((PreferenceScreen) preference);
    }

    return false;
}

从这里我开始研究什么是PreferencesScreen,我很伤心地发现它只是一个对话框的包装器。接下来,我设置操作栏显示选项并尝试找到主页按钮区域。不幸的是,这并不容易获得,但是在层次结构查看器的帮助下,我设法通过找到主页图标,然后找到其父视图来访问。一旦我们可以访问包含的LinearLayout,我们就可以附加一个onClickListener,在那里我们关闭了PreferencesScreen的对话框,该对话框调用了OptiveScreen的onDismissListener并将我们返回到上一个屏幕。

/** Sets up the action bar for an {@link PreferenceScreen} */
public static void initializeActionBar(PreferenceScreen preferenceScreen) {
    final Dialog dialog = preferenceScreen.getDialog();

    if (dialog != null) {
        // Inialize the action bar
        dialog.getActionBar().setDisplayHomeAsUpEnabled(true);

        // Apply custom home button area click listener to close the PreferenceScreen because PreferenceScreens are dialogs which swallow
        // events instead of passing to the activity
        // Related Issue: https://code.google.com/p/android/issues/detail?id=4611
        View homeBtn = dialog.findViewById(android.R.id.home);

        if (homeBtn != null) {
            OnClickListener dismissDialogClickListener = new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            };

            // Prepare yourselves for some hacky programming
            ViewParent homeBtnContainer = homeBtn.getParent();

            // The home button is an ImageView inside a FrameLayout
            if (homeBtnContainer instanceof FrameLayout) {
                ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();

                if (containerParent instanceof LinearLayout) {
                    // This view also contains the title text, set the whole view as clickable
                    ((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener);
                } else {
                    // Just set it on the home button
                    ((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
                }
            } else {
                // The 'If all else fails' default case
                homeBtn.setOnClickListener(dismissDialogClickListener);
            }
        }    
    }
}

答案 2

推荐