2 nextmrwang nextmrwang 于 2016.05.07 05:05 提问

android ActionBarDrawerToggle的问题 ps:新手

先点击用户按钮,再点击开关按钮就变成这样了
左侧边栏被右侧边栏遮住了,试过onDrawerOpened,onDrawerClosed,onDrawerStateChanged,但那都是状态发生改变之后的事,能不能像点击用户头像判定打开,可是我又找不到开关按钮id T T

图片说明

package cn.edu.zafu.toolbardemo;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

import java.lang.reflect.Method;

public class MainActivity extends ActionBarActivity {
protected Toolbar toolbar;
protected DrawerLayout mDrawerLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("");//设置标题
    toolbar.setLogo(R.mipmap.base_common_default_icon_big);//设置logo
    setSupportActionBar(toolbar);//设置toolbar
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);

    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.open, R.string.close) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);

        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);

        }
    };
    mDrawerToggle.syncState();
    mDrawerLayout.setDrawerListener(mDrawerToggle);//设置监听器



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    //点击的是个人中心
    if (id == R.id.action_zone) {
        return toggleDrawerLayout();
    }

    return super.onOptionsItemSelected(item);
}

protected boolean toggleDrawerLayout(){
    //如果左边的已打开,则关闭左边的,不进行后续操作
    if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
        mDrawerLayout.closeDrawer(Gravity.START);
        return true;
    }
    //如果左边的没打开,右边的打开了关闭,关闭了打开
    if (mDrawerLayout.isDrawerOpen(Gravity.END)) {
        mDrawerLayout.closeDrawer(Gravity.END);
    } else {
        mDrawerLayout.openDrawer(Gravity.END);
    }
    return true;
}
/**
**显示溢出菜单图标
**/
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
    if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
        if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
            try {
                Method m = menu.getClass().getDeclaredMethod(
                        "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    return super.onMenuOpened(featureId, menu);
}

}

1个回答

CSDNXIAOD
CSDNXIAOD   2016.05.07 05:12

Android 新手,第一个问题
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!