亲爱的,
我是Android新手,并尝试在我的项目中使用SlidingMenu。 我想创建一个带有两个与Facebook相同的滑动菜单的应用程序。
通过这些步骤,我创建了我的应用程序:
SlidingActivity.java
:
public class SlidingActivity extends SherlockActivity implements SlidingActivityBase
SlidingActivity
:
public class MainMenu extends SlidingActivity
我的主要活动代码:
import android.app.ActionBar;
import android.os.Bundle;
import android.view.KeyEvent;
import com.actionbarsherlock.view.MenuInflater;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity;
public class MainMenu extends SlidingActivity {
SlidingMenu menu;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (enableHomeIconActionBack() || enableHomeIconActionSlidingMenu()) {
ActionBar actionBar = getActionBar();
if (actionBar != null){
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
setContentView(R.layout.activity_main_menu);
setBehindContentView(R.layout.activity_main_menu);
menu = new SlidingMenu(this);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setMode(SlidingMenu.LEFT_RIGHT);
menu.setShadowWidthRes(R.dimen.sliding_menu_shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.sliding_menu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
menu.setMenu(R.layout.sliding_menu_frame);
menu.setSecondaryMenu(R.layout.vehicles_tree);
menu.setSecondaryShadowDrawable(R.drawable.shadow);
}
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (enableHomeIconActionSlidingMenu()
&& menu != null) {
menu.toggle();
} else if (enableHomeIconActionBack()) {
onCustomBackPressed();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
onCustomBackPressed();
return true;
default:
return super.onKeyDown(keyCode, event);
}
}
// If sliding menu is showing, we need to hide it on the first back button
// press.
private void onCustomBackPressed() {
if (menu != null
&& menu.isMenuShowing()) {
menu.toggle();
} else {
this.onBackPressed();
}
}
/**
* Sets activity home icon to have up icon and on press act as device back
* button press.
*
* @return Activation state.
*/
public boolean enableHomeIconActionBack() {
return true;
}
/**
* Sets activity home icon to be as a sliding menu invoke icon and on press
* call toggle command for the sliding menu.
*
* @return Activation state.
*/
public boolean enableHomeIconActionSlidingMenu() {
return true;
}
}
现在,menu.setMenu(R.layout.sliding_menu_frame);
将显示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFF00FF" >
</RelativeLayout>
menu.setSecondaryMenu(R.layout.vehicles_tree);
将显示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0F00FF">
</RelativeLayout>
以下快照描述了这种奇怪的行为:
setBehindContentView(R.layout.activity_main_menu);
的活动,我注意到通过更改附加的布局。 我不想要第四个屏幕,我做错了什么?
此致,, 阿里阿拉斯瓦德
答案 0 :(得分:1)
更改此行
public class MainMenu extends SlidingActivity {
到这一行
public class MainMenu extends Activity {
SlidingActivity将行为编号4与“setBehindContentView”扩展到正常的android“Activity”,因此将其更改回android“Activity”将删除该不需要的行为
答案 1 :(得分:0)
setBehindContentView(R.layout.activity_main_menu);
这会设置主菜单的布局(向左滑动时会发生什么),因此您应将其更改为使用R.layout.sliding_menu_frame
并删除setMenu(R.layout.sliding_menu_frame)
我不确定为什么调用setMenu
不会覆盖它,但它似乎没有这样做。
答案 2 :(得分:0)
这是一个Android库,可让您轻松创建带有滑入式菜单的应用程序。您可以在Android应用中使用它,前提是您引用此项目并在您的应用中包含许可。谢谢!