本文介绍了如何在首选项中设置导航抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,每个抽屉都有活动,但是当我在首选项"活动中设置抽屉时,我的活动运行正常,但抽屉未显示.

In my app i have each activity with drawer, but when i set drawer in my Preference activity, my activity working fine, but drawer not appear.

这是我的代码.

public class SettingActivity extends PreferenceActivity implements NavigationView.OnNavigationItemSelectedListener {
private AppCompatDelegate mDelegate;

private NavigationView mDrawer;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;

@Override
public void onCreate(Bundle savedInstanceState) {
    getDelegate().installViewFactory();
    getDelegate().onCreate(savedInstanceState);

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    super.onCreate(savedInstanceState);

    if(savedInstanceState == null){
        android.app.Fragment infoFragment = new MyPreferenceFragment();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.add(R.id.fragment_container, infoFragment);
        ft.commit();
    }

}

public static class MyPreferenceFragment extends PreferenceFragment {

    private AppSettings settings;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref);
        Preference ringtonePrefs = findPreference("ringPref");
        ringtonePrefs.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                preference.setSummary(String.valueOf(newValue));
                return true;
            }
        });

        Preference mapTypePrefs = findPreference("mapPref");
        mapTypePrefs.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                preference.setSummary(String.valueOf(newValue));
                return true;
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        settings = AppSettings.getSettings(getActivity());
        Preference ringtonPrefs = findPreference("ringPref");
        ringtonPrefs.setSummary(settings.getRingtone());

        Preference maptypePrefs = findPreference("mapPref");
        maptypePrefs.setSummary(settings.getMapType());
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(sharedPrefsChangeListener);
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(sharedPrefsChangeListener);
    }

    private final SharedPreferences.OnSharedPreferenceChangeListener sharedPrefsChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            settings.load();
        }
    };

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish();
        return true;
    }
    return false;
}

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

}

public android.support.v7.app.ActionBar getSupportActionBar() {
    return getDelegate().getSupportActionBar();
}

public void setSupportActionBar(@Nullable Toolbar toolbar) {
    getDelegate().setSupportActionBar(toolbar);
}

@Override
public MenuInflater getMenuInflater() {
    return getDelegate().getMenuInflater();
}

@Override
public void setContentView(@LayoutRes int layoutResID) {
    getDelegate().setContentView(layoutResID);
}

@Override
public void setContentView(View view) {
    getDelegate().setContentView(view);
    setContentView( view, new DrawerLayout.LayoutParams( DrawerLayout.LayoutParams.MATCH_PARENT, DrawerLayout.LayoutParams.MATCH_PARENT ) );
}

@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
    getDelegate().setContentView(view, params);
    super.setContentView(R.layout.navigation_view);

    setupDrawer();

    mDrawerLayout.addDrawerListener(mDrawerToggle);
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });
}

@Override
public void addContentView(View view, ViewGroup.LayoutParams params) {
    getDelegate().addContentView(view, params);
}

@Override
protected void onPostResume() {
    super.onPostResume();
    getDelegate().onPostResume();
}

@Override
protected void onTitleChanged(CharSequence title, int color) {
    super.onTitleChanged(title, color);
    getDelegate().setTitle(title);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    getDelegate().onConfigurationChanged(newConfig);
}

@Override
protected void onStop() {
    super.onStop();
    getDelegate().onStop();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    getDelegate().onDestroy();
}

public void invalidateOptionsMenu() {
    getDelegate().invalidateOptionsMenu();
}

private AppCompatDelegate getDelegate() {
    if (mDelegate == null) {
        mDelegate = AppCompatDelegate.create(this, null);
    }
    return mDelegate;
}

private void setupDrawer() {
    assert getSupportActionBar() != null;
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    mDrawer = (NavigationView) findViewById(R.id.mNavDrawer);
    assert mDrawer != null;
    mDrawer.setNavigationItemSelectedListener(this);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.string.DrawerOpen,
            R.string.DrawerClose){
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

    };

    mDrawerLayout.addDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    Intent intent = null;
 /*   if (item.getItemId() == R.id.navigation_item_1) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
        intent = new Intent(MapsActivity.this, LocationList.class);
        intent.putExtra("longitude_key", longitude);
        intent.putExtra("latitude_key", latitude);
        intent.putExtra("place_name", placeName);
        intent.putExtra("place_address", address);
        startActivity(intent);
        return true;
    }*/
    if (item.getItemId() == R.id.navigation_item_2) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
        intent = new Intent(this, AboutUs.class);
        startActivity(intent);
        return true;
    }
    if (item.getItemId() == R.id.navigation_item_3) {
        mDrawerLayout.closeDrawer(GravityCompat.START);
        intent = new Intent(this, Help.class);
        startActivity(intent);
        return true;
    }
    if(item.getItemId() == R.id.navigation_item_4){
        mDrawerLayout.closeDrawer(GravityCompat.START);
        Intent i = new Intent(this, SettingActivity.class);
        startActivity(i);
        return true;
    }
    mDrawerLayout.closeDrawers();
    return true;
}

}

这是我的navigation_view.xml

this is my navigation_view.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layoutS"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <FrameLayout
       android:id="@+id/fragment_container"
       android:layout_width="match_parent"
       android:layout_height="match_parent"></FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/mNavDrawerS"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemIconTint="@color/ColorAccent"
        app:itemTextColor="@color/TextColor.DarkGray"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/menu_drawer"/>

</android.support.v4.widget.DrawerLayout>

请告诉我我在这里缺少什么..或告诉我其他方法.

please tell me what i m missing here.. or tell me any other way to do it.

推荐答案

在活动的最低层级布局(默认情况下为content_main)中,添加一个名为fragment_container的FrameLayout.将宽度和高度设置为match_parent.这里的技巧是按照以下步骤创建您的片段:

In your activity's lowest hiearchical layout (content_main by default), add a FrameLayout named fragment_container. Set width and height to match_parent. Trick here is to create your fragment as this:

getFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new MyFragment())
.commit();

因此,您基本上不需要在屏幕上添加片段,只需用片段替换预定义的布局即可.这样可以避免有关抽屉和工具栏的问题.如果没有,请告诉我,以便我进一步调查.

So you basically don't add a fragment to the screen, you just replace a pre-defined layout with a fragment. This prevents the issues regarding the drawer and toolbar. If not, let me know so I can investigate further.

这篇关于如何在首选项中设置导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 16:16