7

我不知道如何在PreferenceScreen. 向上按钮会在您的应用图标旁边的操作栏中显示一个插入符号,允许您导航应用的层次结构,更多信息请点击此处

我有一个首选项片段,当我的主要活动打开时显示,我可以通过添加此行“getActionBar().setDisplayHomeAsUpEnabled(true);”来显示向上按钮:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getFragmentManager().beginTransaction()
    .replace(android.R.id.content, new SettingsFragment())
    .commit();

这会导致向上按钮显示在首选项片段中,但我只想在PreferenceScreens打开其中一个按钮时显示向上按钮,允许导航到 main PreferenceFragment

我的应用程序类似于主要设置应用程序。只有从主设置应用程序打开的子屏幕(例如位置访问)具有向上箭头。

在此处输入图像描述

4

3 回答 3

14

从这个问题,我只是将这两个代码块添加到我的偏好片段中:

@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;
}

和这个:

 /** 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);
            }
        }    
    }
}
于 2013-08-12T22:30:21.827 回答
5

如果您的完整应用程序是一个首选项屏幕,那么您可以使您的主要活动 aPreferenceActivity并且子级别可以是片段。这样,默认情况下,“向上”功能将是您正在寻找的功能。

于 2013-08-14T20:01:15.970 回答
0

看看这个

对于 XML:

<Preference android:title="Acts like a button"
            android:key="button"
            android:summary="This will act like a button"/>

然后对于你的 JavaonCreate()

Preference button = (Preference)findPreference("button");

button.setOnPreferenceClickListener(
    new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference arg0) { 
            //code for what you want it to do   
            return true;
        }
});
于 2013-08-12T20:57:06.450 回答