Fragment placed on top of NavigationDrawer

只谈情不闲聊 提交于 2019-12-12 13:38:31

问题


I'm trying to add a PreferenceFragmentin my application. The problem is, it's auto placed on top of my NavigationDrawer.

public class SetPreferenceActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.settings);
  navigationDrawer(); // Loads the NavigationDrawer

  getFragmentManager().beginTransaction().replace(android.R.id.content,
               new Settings()).commit();

 }

As you can see I load the "SettingsFragment" and replace the content with it? (I'm unsure) but it places it on top of everything else.. Here's my Settings fragment.

public class Settings extends PreferenceFragment  {
    static final String TAG = "MAIN";

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
         addPreferencesFromResource(R.xml.preferences);

    }

Everything work as expected, BUT the PreferenceFragment are loaded in front, covering up the NavigationDrawer slideout, I tried calling bringToFront(); on the listview, with no luck.

A picture for reference :

Is it possible to tell the fragment to load behind the listview? I also tought about loading the fragment in a ViewPager, but I get an error that the Pager Adapter wont accept fragments of type PreferenceFragment.


回答1:


Don't replace android.R.id.content, use the the id of the FrameLayout you have in the layout that contains your DrawerLayout.




回答2:


In addition to what adneal said (in case others have the same problem):

The Activity which calls the PreferenceFragment needs to have the setContentView() method if you extend the Activity with your NavigationDrawer:

public class SetPreferenceActivity extends MyNavigationDrawer {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.settings);

  getFragmentManager().beginTransaction().replace(R.id.drawer_frame_layout,
               new Settings()).commit();    
 }

And the settings.xml should only contain a FrameLayout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"/>



回答3:


I had same issue and i resolved it by replacing android.R.id.content to R.id.container.



来源:https://stackoverflow.com/questions/22493713/fragment-placed-on-top-of-navigationdrawer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!