Android: 如何在导航抽屉中实现设置页面?

Android: How to Implement a Settings Page Within a Navigation Drawer?

所以我正在创建一个应用程序,它利用 Navigation Drawer 分成单独的 fragments 来访问每个选项卡。现在,我想知道如何做的事情是,在用户选择“Settings”选项卡后同样打开一个 Settings/Preferences Fragment or Activity(不确定在这种情况下哪个合适) Navigation Drawer 内。由于我不是应用程序开发方面的专家,我在互联网上到处寻找有关如何执行此操作的清晰解释。

下面是导航抽屉的图片:

如果需要任何布局或 class 代码,我很乐意提供。非常感谢任何帮助!

您可以使用PreferenceFragmentCompat,例如:

首先创建一个 Fragment,它扩展 PreferenceFragmentCompat 和文件夹下关联的布局 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>

请务必将新设置 activity 添加到您的清单中

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);

希望这对您有所帮助。 对不起我的英语。