Add up button to PreferenceScreen

ε祈祈猫儿з 提交于 2019-12-04 21:40:49

问题


I can't figure out how I would go about implementing an up button in a PreferenceScreen. An up button displays a caret in your action bar next to your app icon that allows you to navigate the app's hierarchy, more info here.

I have a Preference Fragment that displays when my main activity is opened and I can get the up button to display by adding this line " getActionBar().setDisplayHomeAsUpEnabled(true);":

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

This causes the up button to display in the Preference Fragment, but I only want to show the up button when one of my PreferenceScreens is opened, allowing navigation to the main PreferenceFragment.

My app is analogous to the main settings app. Only the child screens, like Location Access, that opens from the main Settings app has the up arrow.


回答1:


If your complete application is a preferences screen, then you can make your main activity a PreferenceActivity and the sub-levels can be fragments. This way the 'up' functionality is going to be by default what you are looking for.




回答2:


From this question, I simply added these two code blocks to my Preference Fragment:

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

And this one:

 /** 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);
            }
        }    
    }
}



回答3:


Have a look at this.

For the XML:

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

Then for the Java in your onCreate()

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


来源:https://stackoverflow.com/questions/18155036/add-up-button-to-preferencescreen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!