Appbar覆盖我的设置片段

时间:2015-10-29 18:16:12

标签: java android xml android-fragments

我正在尝试开发Android应用程序而且我遇到了问题。我有一个导航抽屉活动,当我点击抽屉中的一个项目时,它会动态地将片段从java代码添加到给定的活动中。这与我的新闻片段完美配合,但是当我想添加我的设置片段(这是一个PreferenceFragment)时,存在一个小问题:appbar覆盖了我的PreferenceFragment的顶部(内容)。如何让我的PreferenceFragment保持(显示)在appbar下方?提前谢谢!

这是我的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>

这是我将Settings片段添加到我的活动中的方式:

fragmentTransaction.add(R.id.fragment_container, settingsFragment).commit();

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

1 个答案:

答案 0 :(得分:0)

The solution was to add an empty RelativeLayout element to the app_bar_main.xml layout file, which would look something like this:

<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: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>

<RelativeLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
</RelativeLayout>