内的最小
sdk版本。最高为10,不得高于10,否则绝握无法现实在系统导航栏旁。个人想法是:Android3.0后加入了ActionBar控件和虚拟的按键,而ActionBar整合了menu功能导致android3.0以盯宏让上的menu按钮只能现实在ActionBar上,无法显凯局示在系统导航栏旁。
在Launcher.java:private static final int MENU_GROUP_WALLPAPER = 1
/// M: menu group for settings and apps item, add for OP09.
private static final int MENU_GROUP_SETTINGS = 2
private static final int MENU_GROUP_APPS = 3
private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1
private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1
private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1
private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (isWorkspaceLocked()) {
return false
}
super.onCreateOptionsMenu(menu)
Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS)
manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS)
settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED)
String helpUrl = getString(R.string.help_url)
Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl))
help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
.setIcon(android.R.drawable.ic_menu_gallery)
.setAlphabeticShortcut('W')
menu.add(MENU_GROUP_APPS, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
.setIcon(android.R.drawable.ic_menu_manage)
.setIntent(manageApps)
.setAlphabeticShortcut('M')
menu.add(MENU_GROUP_SETTINGS, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
.setIcon(android.R.drawable.ic_menu_preferences)
.setIntent(settings)
.setAlphabeticShortcut('P')
if (!helpUrl.isEmpty()) {
menu.add(0, MENU_HELP, 0, R.string.menu_help)
.setIcon(android.R.drawable.ic_menu_help)
.setIntent(help)
.setAlphabeticShortcut('H')
}
return true
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu)
/// M: show options menu when in workspace state
if (mState == State.WORKSPACE &&!mWorkspace.isInOverviewMode()) {
mWorkspace.enterOverviewMode()
}else if (mState==State.APPS_CUSTOMIZE){
return true
}
return false
}
android 中让菜单menu显示在左上角,可以使用popupwindow技术,也就是悬浮菜单,设置默认的位置为左上角,如下代码:
package com.example.menutypeimport android.app.ActionBar.LayoutParams
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.PopupWindow
import android.widget.PopupWindow.OnDismissListener
import android.widget.TextView
import android.widget.Toast
/**
*
* @author tr
* @time 2014-3-10
* @description 自定义菜单,下拉菜单样式,添加动画效果,重写onMenuOpened()方法,自定义"menu"按键d出菜单
*/
public class MainActivity extends 简卖Activity implements OnClickListener{
野咐此private static Toast mToast
private static Context mContext
private PopupWindow popupWindow
private Button btn_popupwindow
private View mPopupWindowView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mContext = this
btn_popupwindow = (Button) findViewById(R.id.btn_popupwindow)
btn_popupwindow.setOnClickListener(this)
initPopupWindow()
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
/**actionBar上更多按钮*/
getMenuInflater().inflate(R.menu.main, menu)
/**点击menu,d出菜单*/
/*
*
* add()方法的四个参数,依次是:
*
* 1、组别,如果不分组的话就写Menu.NONE,
*
* 2、Id,这个很重要,Android根据这个Id来确定不同的菜单
*
* 3、顺序,那个菜单现在在前面由这个参数的大小决定
*
* 4、文本,菜单的显示文本
*/
menu.add(Menu.NONE, Menu.FIRST + 1, 1, getResource(R.string.edit_text)).setIcon(
R.drawable.ic_edit)
// setIcon()方法为菜单设置图标,这里使用的是系统自带的图标,同学们留意一下,以
// android.R开头的资源是系统提供的,我们自己提供的资源是以R开头的
menu.add(Menu.NONE, Menu.FIRST + 2, 2, getResource(R.string.file)).setIcon(
R.drawable.ic_menu_file)
menu.add(Menu.NONE, Menu.FIRST + 3, 3, getResource(R.string.about)).setIcon(
R.drawable.ic_menu_about)
return true
}
/**菜单打开时调颂迅用*/
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
// TODO Auto-generated method stub
showToast("menu菜单打开:"+featureId)
//点击"menu"按钮打开
if(featureId == 0){
showPopupWindow()
}
return super.onMenuOpened(featureId, menu)// 返回为true 则显示系统menu
// return false
}
/**menu菜单关闭时调用*/
@Override
public void onOptionsMenuClosed(Menu menu) {
// TODO Auto-generated method stub
super.onOptionsMenuClosed(menu)
showToast("menu菜单关闭")
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case Menu.FIRST + 1:
case R.id.action_edit:
showToast(getResource(R.string.edit_text))
break
case Menu.FIRST + 2:
case R.id.action_file:
showToast(getResource(R.string.file))
break
case R.id.action_favorite:
showToast(getResource(R.string.favorite))
break
case R.id.action_share:
showToast(getResource(R.string.share))
break
case Menu.FIRST + 3:
case R.id.action_about:
showToast(getResource(R.string.about))
break
}
return super.onOptionsItemSelected(item)
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_popupwindow:
showPopupWindow()
break
case R.id.textview_about:
showToast(getResource(R.string.about))
popupWindow.dismiss()
break
case R.id.textview_edit:
showToast(getResource(R.string.edit_text))
popupWindow.dismiss()
break
case R.id.textview_file:
showToast(getResource(R.string.file))
popupWindow.dismiss()
break
}
}
/**显示popupwindow*/
private void showPopupWindow(){
if(!popupWindow.isShowing()){
popupWindow.showAsDropDown(btn_popupwindow, btn_popupwindow.getLayoutParams().width/2, 0)
}else{
popupWindow.dismiss()
}
}
/**
* 初始化popupwindow
*/
private void initPopupWindow(){
initPopupWindowView()
//初始化popupwindow,绑定显示view,设置该view的宽度/高度
popupWindow = new PopupWindow(mPopupWindowView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)
popupWindow.setFocusable(true)
popupWindow.setOutsideTouchable(true)
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景;使用该方法点击窗体之外,才可关闭窗体
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bitmap_book_read_chapterlist_repeat))
//Background不能设置为null,dismiss会失效
// popupWindow.setBackgroundDrawable(null)
//设置渐入、渐出动画效果
// popupWindow.setAnimationStyle(R.style.popupwindow)
popupWindow.update()
//popupWindow调用dismiss时触发,设置了setOutsideTouchable(true),点击view之外/按键back的地方也会触发
popupWindow.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
// showToast("关闭popupwindow")
}
})
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)