如何添加可点击的菜单抽屉?

时间:2017-05-02 12:38:56

标签: android menu drawer

我有这个代码来创建一个菜单抽屉,但我不知道如何在菜单中点击一下按钮来切换布局。有人可以帮助我吗?

我的其他Xml文件是xml1和xml2。

这是我的代码:

public class MainActivity extends AppCompatActivity implements TextToSpeech.OnInitListener, View.OnClickListener {

    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.Toolbar);
        setSupportActionBar(mToolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.DrawerLayout);
        mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.closed);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();


        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        NavigationView nv = (NavigationView) findViewById(R.id.nav1);
        nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override public boolean onNavigationItemSelected(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case (R.id.nav_sages):
                        Intent in = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity( in );
                    case (R.id.nav_Satz):
                        Intent int1 = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(int1);
                    case (R.id.nav_auserdem):
                        Intent in2 = new Intent(getApplicationContext(), MainActivity.class);
                        startActivity(in2);
                }

                return true;
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (mToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

1 个答案:

答案 0 :(得分:0)

嘿,你这边做错了,

Intent in = new Intent("here you need to mention your current 
activity,getApplicationContext() works here no problem","and here you need to 
mention 2nd activity, i.e., the activity which you want to navigate");
            startActivity(in);

例如:考虑两个活动MainActivity& Main2Activity; 所以现在如果您在MainActivity中并想要导航到Main2Activity:代码将如下

Intent in = new Intent(getApplicationContext(),Main2Activity.class);
            startActivity(in);

或者你也可以这样做:

Intent in = new Intent(MainActivity.this,Main2Activity.class);
            startActivity(in);

希望它能帮助