add action bar with back button in preference activity

前端 未结 4 2047
遇见更好的自我
遇见更好的自我 2021-01-04 10:00

Here is my preference activity:

package com.example.hms.test;

import android.os.Bundle;
import android.preference.PreferenceActivity;

public class PrefsAct         


        
4条回答
  •  被撕碎了的回忆
    2021-01-04 10:37

    okay. now you still trouble then i have unique solution for you which work 100% with just minor changes.

    so first of all create one style for only settings activity.

    here is my toolbar styles code.

    
    

    and here is my stlyes.xml looks like

        
        
        
    
        
    
           
    
    
    

    yes. you noticed i have create duplicate styles(appTheme and Toolbar_settings_style both are same styles attribute) for only settingsActivity. it is very important to create duplicate styles.

    Now go to your settingsActivity and paste below code inside onCreate().

     getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    

    After placing above code now my SettingsActivity looks like

    import android.media.Ringtone;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.preference.EditTextPreference;
    import android.preference.ListPreference;
    import android.preference.Preference;
    import android.preference.PreferenceFragment;
    import android.preference.PreferenceManager;
    import android.preference.RingtonePreference;
    import android.support.v7.app.ActionBar;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.Toolbar;
    import android.text.TextUtils;
    import android.view.MenuItem;
    
    
    import settingspreferences.AppCompatPreferenceActivity;
    
    public class User_Settings extends AppCompatPreferenceActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
            // load settings fragment
            getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit();
        }
    
        public static class MainPreferenceFragment extends PreferenceFragment {
            @Override
            public void onCreate(final Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.settings);
    
                // gallery EditText change listener
                bindPreferenceSummaryToValue(findPreference(getString(R.string.key_gallery_name)));
    
                // notification preference change listener
    
            }
    
            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                if (item.getItemId() == android.R.id.home) {
                 //   onBackPressed();
                }
                return super.onOptionsItemSelected(item);
            }
    
            private static void bindPreferenceSummaryToValue(Preference preference) {
                preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
    
                sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                        PreferenceManager
                                .getDefaultSharedPreferences(preference.getContext())
                                .getString(preference.getKey(), ""));
            }
    
            /**
             * A preference value change listener that updates the preference's summary
             * to reflect its new value.
             */
            private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue) {
                    String stringValue = newValue.toString();
    
                    if (preference instanceof ListPreference) {
                        // For list preferences, look up the correct display value in
                        // the preference's 'entries' list.
                        ListPreference listPreference = (ListPreference) preference;
                        int index = listPreference.findIndexOfValue(stringValue);
    
                        // Set the summary to reflect the new value.
                        preference.setSummary(
                                index >= 0
                                        ? listPreference.getEntries()[index]
                                        : null);
    
                    } else if (preference instanceof RingtonePreference) {
                        // For ringtone preferences, look up the correct display value
                        // using RingtoneManager.
                        if (TextUtils.isEmpty(stringValue)) {
                            // Empty values correspond to 'silent' (no ringtone).
                            preference.setSummary(R.string.pref_ringtone_silent);
    
                        } else {
                            Ringtone ringtone = RingtoneManager.getRingtone(
                                    preference.getContext(), Uri.parse(stringValue));
    
                            if (ringtone == null) {
                                // Clear the summary if there was a lookup error.
                              //  preference.setSummary(R.string.summary_choose_ringtone);
                            } else {
                                // Set the summary to reflect the new ringtone display
                                // name.
                                String name = ringtone.getTitle(preference.getContext());
                                preference.setSummary(name);
                            }
                        }
    
                    } else if (preference instanceof EditTextPreference) {
                        if (preference.getKey().equals("key_gallery_name")) {
                            // update the changed gallery name to summary filed
                            preference.setSummary(stringValue);
                        }
                    } else {
                        preference.setSummary(stringValue);
                    }
                    return true;
                }
            };
        }
    
    }
    

    Now let's come to real important part. go to manifest.xml file and find your settingsActivity and paste below code.

     
    

    so here is my AndroidManifest.xml looks like

    
    
    
        
    
        
    
            
            
                
                    
    
                    
                
                
                
                
    
                
            
            
            
            
            
        
    
    
    

    This is all you need to shows Toolbar on settingsActivity.

提交回复
热议问题