如何实现从右侧打开抽屉菜单

Posted

技术标签:

【中文标题】如何实现从右侧打开抽屉菜单【英文标题】:How to implement drawer menu open from right side 【发布时间】:2015-09-07 12:48:26 【问题描述】:

我在this tutorial 之后创建了一个导航抽屉

但我需要的是,我需要从右侧打开相同的抽屉菜单。有人知道如何实现吗?

下面是我的xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_
android:layout_
android:fitsSystemWindows="true"
tools:context=".MainActivity">

<LinearLayout
    android:layout_
    android:layout_
    android:orientation="vertical"
    >
    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar"
    />
    <FrameLayout
        android:id="@+id/frame"
        android:layout_
        android:layout_>

    </FrameLayout>

</LinearLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_
    android:layout_
    android:layout_gravity="right"
    app:headerLayout="@layout/header"
    app:menu="@menu/drawer"
    />

在代码中

  // Initializing Drawer Layout and ActionBarToggle
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
    ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer)

        @Override
        public void onDrawerClosed(View drawerView) 
            // Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
            super.onDrawerClosed(drawerView);
        

        @Override
        public boolean onOptionsItemSelected(MenuItem item) 
            if (item != null && item.getItemId() == android.R.id.home) 
                if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) 
                    drawerLayout.closeDrawer(Gravity.RIGHT);
                 else 
                    drawerLayout.openDrawer(Gravity.RIGHT);
                
            
            return false;
        

        @Override
        public void onDrawerOpened(View drawerView) 
            // Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank

            super.onDrawerOpened(drawerView);
        

    ;

【问题讨论】:

How can I open navigation drawer from right side to left的可能重复 检查这个> ***.com/questions/17156340/… 检查这个 > ***.com/questions/17156340/… 【参考方案1】:

在导航视图中试试这个

android:layout_gravity="end" 代替 android:layout_gravity="start"

 <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_
        android:layout_
        android:layout_gravity="end"
        app:headerLayout="@layout/header"
        app:menu="@menu/drawer"
        />

//在你的java活动文件中添加这些行,它会工作

 @Override
    public boolean onOptionsItemSelected(MenuItem item) 

        switch (item.getItemId()) 
            case android.R.id.home:
                if (mDrawer.isDrawerOpen(Gravity.RIGHT)) 
                    mDrawer.closeDrawer(Gravity.RIGHT);
                 else 
                    mDrawer.openDrawer(Gravity.RIGHT);
                
                return true;
            case R.id.action_settings:
                return true;
        

最终更新:

我在您的示例项目(MainActivity.java)中更改了一些行,现在它正在工作,请使用此代码。

不要忘记在您的 xml 文件中写 android:layout_gravity="end" 代替 android:layout_gravity="start"

package com.android4dev.navigationview;


import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity 

    Toolbar toolbar;
    NavigationView navigationView;
    DrawerLayout drawerLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar=(Toolbar)findViewById(R.id.toolbar);
        navigationView=(NavigationView)findViewById(R.id.navigation_view);
        drawerLayout=(DrawerLayout)findViewById(R.id.drawer);


        // Set a Toolbar to replace the ActionBar.
        setToolbarAsActionBar();
        // Setup drawer view
        setupDrawerContent(navigationView);

        // Set the menu icon instead of the launcher icon.
        final ActionBar ab = getSupportActionBar();
        ab.setHomeAsUpIndicator(R.drawable.ic_menu);
        ab.setDisplayHomeAsUpEnabled(true);
        //ab.setDisplayShowTitleEnabled(false);

        Menu menu = navigationView.getMenu();
        MenuItem item = menu.findItem(R.id.starred);
        selectDrawerItem(item);

    

    private void setToolbarAsActionBar() 
        setSupportActionBar(toolbar);
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 

        switch (item.getItemId()) 
            case android.R.id.home:
                if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) 
                    drawerLayout.closeDrawer(Gravity.RIGHT);
                 else 
                    drawerLayout.openDrawer(Gravity.RIGHT);
                
                return true;
            case R.id.action_settings:
                return true;
        
        return super.onOptionsItemSelected(item);
    

    private void setupDrawerContent(NavigationView navigationView) 

        navigationView.setNavigationItemSelectedListener(
                new NavigationView.OnNavigationItemSelectedListener() 
                    @Override
                    public boolean onNavigationItemSelected(MenuItem menuItem) 

                        //Checking if the item is in checked state or not, if not make it in checked state
                        if (menuItem.isChecked()) menuItem.setChecked(false);
                        else menuItem.setChecked(true);

                        selectDrawerItem(menuItem);
                        return true;
                    
                );
    

    public void selectDrawerItem(MenuItem menuItem) 
        // Create a new fragment and specify the planet to show based on
        // position

        switch (menuItem.getItemId()) 

            case R.id.inbox:
                Toast.makeText(getApplicationContext(), "Inbox Selected", Toast.LENGTH_SHORT).show();
                ContentFragment fragment = new ContentFragment();
                android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
                fragmentTransaction.replace(R.id.frame, fragment);
                fragmentTransaction.commit();
                break;

            case R.id.starred:
                Toast.makeText(getApplicationContext(), "Stared Selected", Toast.LENGTH_SHORT).show();
                break;

            case R.id.sent_mail:
                Toast.makeText(getApplicationContext(), "Send Selected", Toast.LENGTH_SHORT).show();
                break;

            case R.id.drafts:
                Toast.makeText(getApplicationContext(), "Drafts Selected", Toast.LENGTH_SHORT).show();
                break;

            case R.id.allmail:
                Toast.makeText(getApplicationContext(), "All Mail Selected", Toast.LENGTH_SHORT).show();
                break;

            case R.id.trash:
                Toast.makeText(getApplicationContext(), "Trash Selected", Toast.LENGTH_SHORT).show();
                break;

            case R.id.spam:
                Toast.makeText(getApplicationContext(), "Spam Selected", Toast.LENGTH_SHORT).show();
                break;

            default:
                Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
                break;

        

        drawerLayout.closeDrawers();
    

    private ActionBarDrawerToggle setupDrawerToggle() 
        return new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer);
    


【讨论】:

试过了,我收到了这个错误。 java.lang.IllegalArgumentException: No drawer view found with gravity LEFT 在滑动时它正在工作,但在点击汉堡图标时,它显示错误java.lang.IllegalArgumentException: No drawer view found with gravity LEFT 我在我的导航抽屉上应用了我的代码,它在这里工作正常.. 唯一的区别是,我没有使用 onOptionItemSelected,我使用的是 setToolbarNavigationClickListener,我正在给你我的代码,让试试看。 我也试过这段代码。但不行 我仍然收到这个错误 java.lang.IllegalArgumentException: No drawer view found withgravity LEFT @user3339689 你能建议工具栏有什么问题吗?【参考方案2】:

这是可能的。您必须在导航抽屉中设置 android:layout_gravity="right" 并更改重力:

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) 

        @Override
        public boolean onOptionsItemSelected(MenuItem item) 
            if (item != null && item.getItemId() == android.R.id.home) 
                if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) 
                    mDrawerLayout.closeDrawer(Gravity.RIGHT);
                 else 
                    mDrawerLayout.openDrawer(Gravity.RIGHT);
                
            
            return false;
        
    ;

应该可以!

【讨论】:

我收到了这个java.lang.IllegalArgumentException: No drawer view found with gravity LEFT 你能发布你的 DrawerLayout 声明和 xml 吗? 在滑动时它正在工作,但在点击汉堡图标时,它显示错误 java.lang.IllegalArgumentException: No Drawer view found withgravity LEFT –【参考方案3】:

实现左侧导航抽屉后,更改以下属性。

DrawerLayout 中的工具:openDrawer="start"

导航视图中的android:layout_gravity="end"

toolbar.setNavigationIcon (R.color.transparent);
mDrawerToggle.setDrawerIndicatorEnabled (false);

toolbar.setNavigationOnClickListener (new View.OnClickListener ()  
    @Override public void onClick(View view)  
        if (mDrawerLayout.isDrawerOpen (Gravity.END)) 
            mDrawerLayout.closeDrawer (Gravity.END); 
         else  
            mDrawerLayout.openDrawer (Gravity.END); 
         
     
 );

【讨论】:

【参考方案4】:

试试这个对我有用

@Override
        public boolean onOptionsItemSelected(MenuItem item) 

            switch (item.getItemId()) 
                case android.R.id.home:
                    if (drawer.isDrawerOpen(Gravity.RIGHT)) 
                        drawer.closeDrawer(Gravity.RIGHT);
                     else 
                        drawer.openDrawer(Gravity.RIGHT);
                    
                    return true;
                case R.id.action_settings:
                    // do what you want
                    return true;
            
            return false;
        

【讨论】:

以上是关于如何实现从右侧打开抽屉菜单的主要内容,如果未能解决你的问题,请参考以下文章

导航抽屉和右侧菜单之间的冲突

如何从片段返回主要活动

翩翩从安卓抽屉菜单中打开页面后如何刷新主页面?

如何在 vuetify 主抽屉/导航中创建子菜单

如何在qt中从右侧创建导航抽屉

如何使向上按钮返回而不是打开导航抽屉