【发布时间】:2016-01-30 00:55:43
【问题描述】:
我正在尝试开发 Android 应用程序,但遇到了问题。我有一个导航抽屉活动,当我从抽屉中单击一个项目时,它会动态地将片段添加到 java 代码中的给定活动。这与我的新闻片段完美配合,但是当我想添加我的设置片段(这是一个 PreferenceFragment)时,有一个小问题:应用栏覆盖了我的 PreferenceFragment 的顶部(内容)。如何让我的 PreferenceFragment 留在(出现)应用栏下方?提前致谢!
这是我的 activity_main.xml 文件。
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="@layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />
这是我的 app_bar_main.xml 文件。
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:id="@+id/fragment_container"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
这就是我将设置片段添加到我的活动的方式:
fragmentTransaction.add(R.id.fragment_container, settingsFragment).commit();
我的 SettingsFragment 如下所示:
public class SettingsFragment extends PreferenceFragment {
private static final boolean ALWAYS_SIMPLE_PREFS = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.pref_notification);
Preference aboutPref = (Preference) findPreference("aboutKey");
aboutPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
// some event handling code
});
Preference notiPref = (Preference) findPreference("notifications");
notiPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
// some event handling code
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
return true;
}
return super.onOptionsItemSelected(item);
}
private static boolean isXLargeTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
private static boolean isSimplePreferences(Context context) {
return ALWAYS_SIMPLE_PREFS
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
|| !isXLargeTablet(context);
}
}
【问题讨论】:
-
你这是什么意思?我有一个扩展 PreferenceFragment 类的 SettingsFragment 类。首先,我实例化一个 SettingsFragment 对象(settingsFragment),然后使用 fragmentTransaction.add() 将该对象添加到 Activity
标签: java android xml android-fragments