将选项添加到PreferenceScreen

时间:2013-08-09 20:22:20

标签: java android android-preferences

我无法弄清楚如何在PreferenceScreen中实现向上按钮。向上按钮会在应用图标旁边的操作栏中显示一条插入符号,可让您浏览应用的层次结构,更多信息here

我有一个Preference Fragment,当我的主要活动被打开时显示,我可以通过添加这行“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之一打开时显示向上按钮,允许导航到主PreferenceFragment

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

enter image description here

3 个答案:

答案 0 :(得分:14)

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

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

答案 1 :(得分:5)

如果您的完整应用程序是首选项屏幕,那么您可以将主要活动设为PreferenceActivity,子级别可以是片段。这样,“向上”功能将默认为您所需要的功能。

答案 2 :(得分:0)

查看this

对于XML:

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

然后是onCreate()

中的Java
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;
        }
});