如何在PreferenceActivity中添加ToolBar?

我想在Android应用程序中的PreferenceActivity中添加ToolBar 。 我写了以下代码。

  public class SettingsActivity extends PreferenceActivity { SendSMS sms; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings); LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent(); android.support.v7.widget.Toolbar bar = (android.support.v7.widget.Toolbar) LayoutInflater.from(this).inflate(R.layout.action_bar_setting, root, false); root.addView(bar, 0); bar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onBackPressed(); } }); 

}

这在我的机器人Kitkat手机API 19中完美运行,但强制关闭API级别10即姜饼 。 请建议我。

你需要一个包含Toolbar的布局,以及一个带有android:id="@android:id/list"ListView

activity_settings.xml

      

SettingsActivity.java

 public class SettingsActivity extends PreferenceActivity { private AppCompatDelegate mDelegate; @Override protected void onCreate(Bundle savedInstanceState) { getDelegate().installViewFactory(); getDelegate().onCreate(savedInstanceState); super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); addPreferencesFromResource(R.xml.preferences); ... } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); getDelegate().onPostCreate(savedInstanceState); } @Override public void setContentView(@LayoutRes int layoutResID) { getDelegate().setContentView(layoutResID); } @Override protected void onPostResume() { super.onPostResume(); getDelegate().onPostResume(); } @Override protected void onStop() { super.onStop(); getDelegate().onStop(); } @Override protected void onDestroy() { super.onDestroy(); getDelegate().onDestroy(); } private void setSupportActionBar(@Nullable Toolbar toolbar) { getDelegate().setSupportActionBar(toolbar); } private AppCompatDelegate getDelegate() { if (mDelegate == null) { mDelegate = AppCompatDelegate.create(this, null); } return mDelegate; } ... } 

看看我的完整工作示例:

  • activity_settings.xml
  • SettingsActivity.java

来自Android团队的参考: AppCompatPreferenceActivity

尝试:

 public class SettingsActivity extends AppCompatPreferenceActivity { ..... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setupActionBar(); /* getFragmentManager().beginTransaction() .replace(android.R.id.content, new GeneralPreferenceFragment()) .commit(); */ //addPreferencesFromResource(R.xml.pref_general); } private void setupActionBar() { ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root); //id from appcompat if (rootView != null) { View view = getLayoutInflater().inflate(R.layout.app_bar_layout, rootView, false); rootView.addView(view, 0); Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); setSupportActionBar(toolbar); } ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { // Show the Up button in the action bar. actionBar.setDisplayHomeAsUpEnabled(true); } } 

app_bar_layout.xml

     

您可以从@android:style/Theme.Material.Light.DarkActionBar轻松添加工具栏

AndroidManifest.xml中

  

v21 / styles.xml中

  

v14 / styles.xml中支持Back API

  

在我的情况下通过膨胀自定义工具栏非常容易和运作良好。 在你的java代码中做如下,

  public class SettingsPrefActivity extends AppCompatPreferenceActivity { // private static final String TAG = SettingsPrefActivity.class.getSimpleName(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setting up toolbar getLayoutInflater().inflate(R.layout.toolbar_setting, (ViewGroup) findViewById(android.R.id.content)); Toolbar toolbar = findViewById(R.id.toolbar); toolbar.setTitle("Settings"); setSupportActionBars(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); // load settings fragment getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit(); } } 

在您的xml端代码中,在顶部添加一个首选项类别,如下所示,

    //put below line at the top of your xml preference layout screen..  

并在您的布局资源文件夹toolbar_setting中,

    

我迟到了,但这是对问题的一个小修复,而不是编写大量的代码或添加庞大的库。

转到AndroidManifest.xml并在Preference Activity中添加自定义theme

   

这是上面添加到@style/Pref的自定义主题

   

希望能帮助到你!

XML

                             

java代码是,

  public class ActivitySettings extends PreferenceActivity { private static final String TAG = "SB_ActivitySettings"; private AppCompatDelegate mDelegate; @Override protected void onCreate(Bundle savedInstanceState) { getDelegate().installViewFactory(); getDelegate().onCreate(savedInstanceState); super.onCreate(savedInstanceState); getFragmentManager().beginTransaction().replace( android.R.id.content, new ActivitySettings.AboutPreferenceFragment()).commit(); } private AppCompatDelegate getDelegate() { if (mDelegate == null) { mDelegate = AppCompatDelegate.create(this, null); } return mDelegate; } protected boolean isValidFragment(String fragmentName) { return PreferenceFragment.class.getName().equals(fragmentName) || AboutPreferenceFragment.class.getName().equals(fragmentName); } @Override protected void onStop() { super.onStop(); getDelegate().onStop(); } @Override protected void onDestroy() { super.onDestroy(); getDelegate().onDestroy(); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); getDelegate().onPostCreate(savedInstanceState); } @Override protected void onPostResume() { super.onPostResume(); getDelegate().onPostResume(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); getDelegate().onConfigurationChanged(newConfig); } @Override public void setContentView(@LayoutRes int layoutResID) { getDelegate().setContentView(layoutResID); } @Override public void setContentView(View view) { getDelegate().setContentView(view); } @Override public void setContentView(View view, ViewGroup.LayoutParams params) { getDelegate().setContentView(view, params); } @Override public void addContentView(View view, ViewGroup.LayoutParams params) { getDelegate().addContentView(view, params); } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { if (!super.onMenuItemSelected(featureId, item)) { NavUtils.navigateUpFromSameTask(this); } return true; } return super.onMenuItemSelected(featureId, item); } public void invalidateOptionsMenu() { getDelegate().invalidateOptionsMenu(); } @NonNull @Override public MenuInflater getMenuInflater() { return getDelegate().getMenuInflater(); } @Override protected void onTitleChanged(CharSequence title, int color) { super.onTitleChanged(title, color); getDelegate().setTitle(title); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) public static class AboutPreferenceFragment extends PreferenceFragment { private final PreferenceListener mListener = new PreferenceListener(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences_list); String keyList[] = getResources().getStringArray(R.array.pref_key_list); Preference preference; String value; for (String pref_key : keyList) { preference = findPreference(pref_key); if (preference != null) { if (preference instanceof ListPreference) { value = ((ListPreference) preference).getValue(); } else if (preference instanceof SwitchPreference) { value = ((SwitchPreference) preference).isChecked() ? "Disabled" : "Enabled"; } else if (preference instanceof RingtonePreference) { value = ((RingtonePreference) preference).getShowSilent() ? "Enabled" : "Silent"; } else { value = ""; } preference.setSummary(value); preference.setOnPreferenceChangeListener(mListener); preference.setOnPreferenceClickListener(mListener); } } } private void showChangeLogDialog() { Utilities.log(TAG, "showChangeLogDialog"); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); WebView webView = new WebView(getActivity()); webView.loadUrl("file:///android_asset/vinoj.html"); builder.setView(webView); builder.setNegativeButton(null, null); builder.setPositiveButton(null, null); builder.show(); } private class PreferenceListener implements Preference.OnPreferenceChangeListener, Preference.OnPreferenceClickListener { @Override public boolean onPreferenceChange(Preference pPreference, Object pObject) { Utilities.log(TAG, "onPreferenceChange: " + pPreference.getKey()); String value; if (pPreference instanceof ListPreference) { value = pObject.toString(); } else if (pPreference instanceof SwitchPreference) { value = ((SwitchPreference) pPreference).isChecked() ? "Disabled" : "Enabled"; } else if (pPreference instanceof RingtonePreference) { value = ((RingtonePreference) pPreference).getShowSilent() ? "Enabled" : "Silent"; } else { value = pObject.toString(); } pPreference.setSummary(value); if (pPreference.getKey().equalsIgnoreCase( getString(R.string.pref_key_theme_dark))) { Utilities.restartApplication(getActivity()); } return true; } @Override public boolean onPreferenceClick(Preference pPreference) { String key = pPreference.getKey(); if (key == null) { Utilities.log(TAG, "onPreferenceClick() returning : key = null"); return false; } Utilities.log(TAG, "onPreferenceClick() called key = [" + key + "]"); if (key.equalsIgnoreCase(getString(R.string.pref_key_changelog))) { showChangeLogDialog(); } return true; } } } 

}

代替:

 public class PreferencesActivity extends Activity 

做这个:

 public class PreferencesActivity extends AppCompatActivity