将操作栏选项菜单对齐到右侧

时间:2013-06-11 11:02:03

标签: android android-actionbar android-optionsmenu navigation-drawer

默认的应用程序图标和选项菜单项显示在操作栏的左侧。我希望选项菜单显示在菜单栏的右侧。

我使用自定义视图在右侧的操作栏上显示其他按钮,但默认选项菜单项仍显示在左侧。

我在实现导航抽屉时基本上面临这个问题。对于我的应用程序,用户应该能够点击位于操作栏右端的菜单项,该菜单项将抽出导航抽屉。目前默认实现在默认情况下点击操作栏左侧显示的选项菜单时调用导航抽屉。

谢谢!

2 个答案:

答案 0 :(得分:0)

this is an xml

    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_gravity="right"

    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <!-- As the main content view, the view below consumes the entire
             space available using match_parent in both dimensions. -->
        <FrameLayout
     android:id="@+id/content_frame"
     android:layout_width="match_parent"
     android:layout_height="match_parent" >



        </FrameLayout>





                <ListView
                 android:id="@+id/right_drawer"
                 android:layout_width="400dp"
                 android:layout_height="600dp"
                 android:choiceMode="singleChoice"
                 android:divider="@android:color/transparent"
                 android:dividerHeight="0dp"
                 android:background="#111"/>






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

答案 1 :(得分:0)

    Java code :

    @Override
onCreate()
{
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mDrawerList = (ListView) findViewById(R.id.right_drawer);

        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
                GravityCompat.START);
        mDrawerLayout.setAlpha((float)0.8);


        mDrawerList.setAdapter(adapter);
        mDrawerList.setClickable(false);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());



mDrawerToggle = new ActionBarDrawerToggle(this, // host Activity
                mDrawerLayout, // DrawerLayout object
                R.drawable.ic_drawer, // nav drawer image to replace 'Up' caret
                R.string.drawer_open, // "open drawer" description for
                                        // accessibility
                R.string.drawer_close // "close drawer" description for
                                        // accessibility
        ) {
            public void onDrawerClosed(View view) {
                // getActionBar().setTitle(mTitle);
                // invalidateOptionsMenu(); // creates call to
                // onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                // getActionBar().setTitle(mDrawerTitle);
                // invalidateOptionsMenu(); // creates call to
                // onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);


        if (savedInstanceState == null) {
            selectItem(0);
        }

}
        public boolean onCreateOptionsMenu(Menu menu) {
            // TODO Auto-generated method stub
            SubMenu filter = menu.addSubMenu(" ");
            subFilter = filter.getItem().setVisible(true);
            filter.setIcon(R.drawable.ico_filter);
            subFilter.setShowAsAction(android.view.MenuItem.SHOW_AS_ACTION_ALWAYS);
            return true;
        }

        public boolean onPrepareOptionsMenu(Menu menu) {
            // If the nav drawer is open, hide action items related to the content
            // view
            boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
            subFilter.setVisible(!drawerOpen);
            return super.onPrepareOptionsMenu(menu);
        }

        public boolean onOptionsItemSelected(MenuItem menuItem) {
            if (menuItem.equals(subFilter)) {
                if (mDrawerLayout.isDrawerOpen(Gravity.END) == true) {
                    mDrawerLayout.closeDrawers();

                } else {
                    mDrawerLayout.openDrawer(Gravity.END);

                }

            } else {
                onBackPressed();

            }

            return true;
        }