您可以使用PreferenceFragmentCompat,例如:
首先,您创建一个扩展 PreferenceFragmentCompat 的 Fragment 和 res/xml 文件夹下关联的布局
settings.xml
<android.support.v7.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.preference.PreferenceCategory
android:key="the_key_to_retrieve_the_preference_in_code"
android:title="the_preference_title">
<android.support.v7.preference.Preference
android:key="key"
android:summary="subtitle"
android:title="title" />
<android.support.v7.preference.Preference
android:key="key2"
android:summary="subtitle2"
android:title="title2" />
<android.support.v7.preference.CheckBoxPreference
android:key="key_for_check_box"
android:summary="subtitle"
android:title="title" />
</android.support.v7.preference.PreferenceCategory>
</android.support.v7.preference.PreferenceScreen>
SettingsFragment.java
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.settings);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// To get a preference
PreferenceScreen preferenceScreen = getPreferenceScreen();
Preference preference = preferenceScreen.findPreference("preference_ key_defined_in_the_xml");
//You can set a listener
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
return false;
}
});
//change title
preference.setTitle("my_title");
// etc
}
}
然后你创建一个 Activity 来保存片段:
SettingsActivity.java
public class SettingsActivity extends AppCompatActivity {
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
setActionBarTitle("Settings");
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
}
settings_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<fragment
android:id="@+id/settings_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:name="package.name.SettingsFragment"/>
</android.support.design.widget.CoordinatorLayout>
确保将新的设置活动添加到您的清单中
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- add the below line to your AndroidManifest.xml -->
<activity android:name=".SettingsActivity"></activity>
</application>
</manifest>
那就是它,然后当您单击 NavigationDrawer 中的项目时,您会设置一个新的 Intent 并调用它:
Intent intent = new Intent(this /*or the actual context*/, SettingsActivity.class);
startActivity(intent);
希望这会有所帮助。
对不起我的英语。