2015-10-29 41 views
1

我正在嘗試開發Android應用程序,並且遇到問題。我有一個導航抽屜活動,當我從抽屜中點擊一個項目時,它會動態地將片段添加到來自Java代碼的給定活動中。這與我的新聞片段完美結合,但是當我想添加我的Settings片段(這是一個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> 

這是我如何設置片段添加到我的活動:

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); 
    } 
} 
+0

你是什麼意思?我有一個SettingsFragment類來擴展PreferenceFragment類。首先,我實例化一個SettingsFragment對象(settingsFragment),然後使用fragmentTransaction.add()將此對象添加到Activity中 – masm64

回答

0

解決的辦法是添加一個空RelativeLayout元素添加到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: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>