Android嵌套了带ActionBar的PreferenceScreen

我的Android应用程序中有一个SettingsActivity。 最初没有Actionbar,所以我实现了这个:

settings_toolbar.xml

  

SettingsActivity.java

 public class SettingsActivity extends PreferenceActivity { @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent(); Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false); root.addView(bar, 0); // insert at top bar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } } 

它工作得很好但仅适用于第一个PreferenceScreen。 如果我有一个嵌套的PreferenceScreen,那么就没有ActionBar。 我怎样才能实现这一点,在嵌套的PreferenceScreen上还有带后退按钮的ActionBar?

它应该与API15 +和AppCombat兼容

原帖: 如何将支持库中的Action Bar添加到PreferenceActivity中?

我们可以使用一个简单的可点击的首选项,而不是使用嵌套的PreferenceScreen,使它像一个“嵌套的标题”一样工作; 这将显示通常的ActionBar,因为它启动了PreferenceActivity实例,因此也将维护单窗格/双窗格导航样式。 这是一些简化的示例代码,其中包括ActionBar的后导航按钮设置:

main_preferences.xml

       

subscreen_preference.xml

     

MyPreferenceActivity.class

 public class MyPreferenceActivity extends AppCompatPreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //display back button. Fragments will handle its behavior (see below) getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override public void onBuildHeaders(List
target) { loadHeadersFromResource(R.xml.pref_headers, target); } @Override protected boolean isValidFragment(String fragmentName) { return MainPreferenceFragment.class.getName().equals(fragmentName) || SubscreenFragment.class.getName().equals(fragmentName); } public static class MainPreferenceFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { //let the fragment intercept the ActionBar buttons: setHasOptionsMenu(true); addPreferencesFromResource(R.xml.main_preferences); findPreference("subscreen_preference").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { //we create a Header manually: Header header = new Header(); //mandatory fragment name: header.fragment = "com.foo.MyPreferenceActivity$SubscreenFragment"; //subscreen title to be shown in the ActionBar header.titleRes = R.string.settings_fragment_title; //this will do the trick, no further action required: //we can ignore the second parameter ((MyPreferenceActivity)getActivity()).onHeaderClick(header, 0); return true; } }); } //this will make the ActionBar back navigation button // behave like the system back button @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { if (!super.onOptionsItemSelected(item)) { getActivity().onBackPressed(); } return true; } return super.onOptionsItemSelected(item); } } public static class SubscreenFragment extends PreferenceFragment { //usual implementation } }

重要提示 :如果您使用Proguard,请记住添加以下规则,否则isInvalidFragment()将返回false:

 -keepnames class com.foo.MyPreferenceActivity$SubscreenFragment