我正在使用这个
link实现滑动菜单.现在,如果我在onCreate()方法中使用此代码,它的工作正常.
我的简单问题是如何在按钮的点击事件上打开滑块?
我使用以下代码.
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final SlidingMenu menu = new SlidingMenu(this); Button mButton = (Button) findViewById(R.id.slidingMenu); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { menu.setMode(SlidingMenu.RIGHT); menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); menu.setBehindOffsetRes(R.dimen.slidingmenu_offset); menu.setFadeDegree(0.5f); menu.attachToActivity(MainActivity.this,SlidingMenu.SLIDING_CONTENT); menu.setMenu(R.layout.activity_menu); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main,menu); return true; } }
解决方法
您应该在单击侦听器之前设置菜单.在您的代码段中,每次单击按钮时都会设置并附加菜单.但是,您应该只显示菜单而不是在点击时附加它.
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final SlidingMenu menu = new SlidingMenu(this); menu.setMode(SlidingMenu.RIGHT); menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); menu.setBehindOffsetRes(R.dimen.slidingmenu_offset); menu.setFadeDegree(0.5f); menu.attachToActivity(MainActivity.this,SlidingMenu.SLIDING_CONTENT); menu.setMenu(R.layout.activity_menu); Button mButton = (Button) findViewById(R.id.slidingMenu); mButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { menu.showMenu(); //or //menu.toggle(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main,menu); return true; } }