Pooya給出的答案不適用於PreferenceActivity。而是讓你的類擴展AppCompatActivity,並使用PreferenceFragment來加載首選項。這裏是我的設置代碼:
public class MyPrefsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
public static class MyPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
}
把活動中的AndroidManifest.xml:
<activity android:name=".MyPrefsActivity"
android:label="Preferences"
android:theme="@style/AppTheme"/>
現在你可以開始使用我的主要活動的意圖設置活動(或任何父活動你有)爲正常:
Intent prefsIntent = new Intent(activity, MyPrefsActivity.class);
activity.startActivity(prefsIntent);
[創建與支持(V21)工具的偏好畫面]的可能的複製(http://stackoverflow.com/questions/26564400/creating-a-preference-screen-with-支持-v 21-工具欄) – Shubhank
嘗試http://stackoverflow.com/a/27455363/3981656 –
也檢查此[將ActionBar添加到PreferenceActivity](http://stackoverflow.com/questions/30793609/add-actionbar-to-a -preferenceactivity) – jayeshsolanki93