简体   繁体   中英

Add ActionBar to a PreferenceActivity

I am trying to make a Settings Activity using a PreferenceActivity . The problem is that the ActionBar won't show up no matter what I do.

I've tried getSupportActionBar , getActionBar , setActionBar . Nothing works. I see other apps settings with ActionBar s. Do they not use PreferenceActivity ?

If you are using an AppCompat theme, you have to pay attention to some points.

The PreferenceActivity doesn't extend the AppCompatActivity or the deprecated ActionBarActivity .

As solution you can create a PreferenceFragment as you are doing and use it in a standard AppCompatActivity . Of course you can use also a Toolbar .

Moreover with the new 22.1+ appcompat you can use the AppCompatDelegate to extend AppCompat's support to any Activity.

You can check this official link to AppCompatPreferenceActivity , where you can find an example of this technique.

By explicitly themeing SettingActivity with a theme derived from DarkActionBar, we are able to add back the Action Bar.

I did the same to have an action bar in my Settings activity and it worked for me .

1.First add a different style for your Settings Activity in styles.xml

<style name="SettingsTheme" parent="AppTheme"/>

2.Then make a seperate styles.xml for the for version 21 (v21/styles.xml) as given below

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Settings activity theme. -->
    <style name="SettingsTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
        <item name="android:colorPrimary">@color/sunshine_blue</item>
        <item name="android:colorPrimaryDark">@color/sunshine_dark_blue</item>
    </style>

</resources> 
  1. At last in the AndroidManifest tag for your SettingsActivity you'll also want to add the settings theme:

    android:theme="@style/SettingsTheme"

This would add an action bar to your Settings activity in devices having android versions grater than or equal to 21.

This is what worked for me,

simply change PreferenceActivity to AppCompatActivity and in your manifest.xml file add parentActivityName for that particular activity.

AndroidManifest.xml

 <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings"
            android:parentActivityName=".HomeActivity"></activity>

SettingsActivty.java

public class SettingsActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();


    }


    public static class MyPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);

        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM