简体   繁体   English

将ActionBar添加到PreferenceActivity

[英]Add ActionBar to a PreferenceActivity

I am trying to make a Settings Activity using a PreferenceActivity . 我正在尝试使用PreferenceActivity进行Settings Activity The problem is that the ActionBar won't show up no matter what I do. 问题是,无论我做什么, ActionBar都不会出现。

I've tried getSupportActionBar , getActionBar , setActionBar . 我试过getSupportActionBargetActionBarsetActionBar Nothing works. 什么都行不通。 I see other apps settings with ActionBar s. 我看到ActionBar的其他应用程序设置。 Do they not use PreferenceActivity ? 他们不使用PreferenceActivity吗?

If you are using an AppCompat theme, you have to pay attention to some points. 如果您使用的是AppCompat主题,则必须注意一些要点。

The PreferenceActivity doesn't extend the AppCompatActivity or the deprecated ActionBarActivity . PreferenceActivity 不会扩展 AppCompatActivity或不推荐使用的ActionBarActivity

As solution you can create a PreferenceFragment as you are doing and use it in a standard AppCompatActivity . 作为解决方案,您可以在执行时创建PreferenceFragment ,并在标准AppCompatActivity 使用它 Of course you can use also a Toolbar . 当然,您也可以使用Toolbar

Moreover with the new 22.1+ appcompat you can use the AppCompatDelegate to extend AppCompat's support to any Activity. 此外,使用新的22.1+ appcompat,您可以使用AppCompatDelegate将AppCompat的支持扩展到任何Activity。

You can check this official link to AppCompatPreferenceActivity , where you can find an example of this technique. 您可以查看AppCompatPreferenceActivity的官方链接,您可以在其中找到此技术的示例。

By explicitly themeing SettingActivity with a theme derived from DarkActionBar, we are able to add back the Action Bar. 通过使用从DarkActionBar派生的主题明确地设置SettingActivity主题,我们可以添加回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 1.首先在styles.xml中为Settings Activity添加不同的样式

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

2.Then make a seperate styles.xml for the for version 21 (v21/styles.xml) as given below 2.然后为版本21(v21 / styles.xml)创建一个单独的styles.xml ,如下所示

<?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: 最后在您的SettingsActivity的AndroidManifest标签中,您还需要添加设置主题:

    android:theme="@style/SettingsTheme" 机器人:主题= “@风格/ SettingsTheme”

This would add an action bar to your Settings activity in devices having android versions grater than or equal to 21. 这将在Android版本大于或等于21的设备中为您的设置活动添加操作栏。

This is what worked for me, 这对我有用,

simply change PreferenceActivity to AppCompatActivity and in your manifest.xml file add parentActivityName for that particular activity. 只需将PreferenceActivity更改为AppCompatActivity,并在manifest.xml文件中为该特定活动添加parentActivityName

AndroidManifest.xml AndroidManifest.xml中

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

SettingsActivty.java 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);

        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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