Android:如何在导航抽屉中实现设置页面?
问题描述:
因此,我正在创建一个应用程序,该应用程序利用分为fragments
的Navigation Drawer
来访问每个选项卡。现在,我想知道该怎么做,在用户选择Navigation Drawer
中的“Settings
”选项卡后,同样有一个Settings/Preferences Fragment or Activity
(不确定哪一个适合在这种情况下)打开。我已经在整个互联网上寻找了关于如何做到这一点的CLEAR解释,因为我不是应用程序开发专家。Android:如何在导航抽屉中实现设置页面?
如果需要任何布局或类代码,我很乐意提供。任何帮助深表感谢!
答
可以使用PreferenceFragmentCompat,例如:你创建
首先片段谁伸出PreferenceFragmentCompat和文件夹下资源相关联的布局/ 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
}
}
然后创建一个活动谁将持有的片段:
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>
说起来,然后当你点击该项目在您的NavigationDrawer
中,您设置了一个新的Intent
并将其命名为:
Intent intent = new Intent(this /*or the actual context*/, SettingsActivity.class);
startActivity(intent);
希望这会有所帮助。 对不起,我的英语。
+0
这么好的答案!非常感谢! –
您应该创建一个设置活动,然后添加/替换'PreferenceFragment' – tahsinRupam
您是否希望PreferenceFragment在抽屉中逐字打开?或者只是从抽屉里进入一个新的活动?或者你想要Home页面等被设置页面替换?如果是这样,为什么? –
@tahsinRupam您是否在创建新活动时指定了图库中的设置活动模板? – AndroidDevBro