自定义菜单


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="134dip"
android:id="@+id/launcher_menu"
android:gravity="bottom"
android:clickable="true"
android:focusable="true"
>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="67dip"
android:orientation="horizontal"
android:gravity="center" >
<LinearLayout android:id="@+id/layout_item_1"
android:layout_width="fill_parent"
android:layout_height="67dip"
android:layout_weight="1"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:background="@drawable/launcher_menu_selector"
android:gravity="center" >
<TextView android:id="@+id/menu_item_text_1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000000"
android:includeFontPadding="false"
android:gravity="center" />
</LinearLayout>
<LinearLayout android:id="@+id/layout_item_2"
android:layout_width="fill_parent"
android:layout_height="67dip"
android:layout_weight="1"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:background="@drawable/launcher_menu_selector"
android:gravity="center" >
<TextView android:id="@+id/menu_item_text_2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000000"
android:includeFontPadding="false"
android:gravity="center" />
</LinearLayout>
<LinearLayout android:id="@+id/layout_item_3"
android:layout_width="fill_parent"
android:layout_height="67dip"
android:layout_weight="1"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:background="@drawable/launcher_menu_selector"
android:gravity="center" >
<TextView android:id="@+id/menu_item_text_3"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000000"
android:includeFontPadding="false"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="67dip"
android:orientation="horizontal"
android:gravity="center" >
<LinearLayout android:id="@+id/layout_item_4"
android:layout_width="fill_parent"
android:layout_height="67dip"
android:layout_weight="1"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:background="@drawable/launcher_menu_selector"
android:gravity="center" >
<TextView android:id="@+id/menu_item_text_4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000000"
android:includeFontPadding="false"
android:gravity="center" />
</LinearLayout>
<LinearLayout android:id="@+id/layout_item_5"
android:layout_width="fill_parent"
android:layout_height="67dip"
android:layout_weight="1"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:background="@drawable/launcher_menu_selector"
android:gravity="center" >
<TextView android:id="@+id/menu_item_text_5"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000000"
android:includeFontPadding="false"
android:gravity="center" />
</LinearLayout>
<LinearLayout android:id="@+id/layout_item_6"
android:layout_width="fill_parent"
android:layout_height="67dip"
android:layout_weight="1"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:background="@drawable/launcher_menu_selector"
android:gravity="center" >
<TextView android:id="@+id/menu_item_text_6"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000000"
android:includeFontPadding="false"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>



@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_MENU){
showLauncherMenu(keyCode, event);
return true;
}
...
}


private PopupWindow menuWindow;
private void showLauncherMenu(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_MENU){
int[] menuItemLayoutResId = {R.id.layout_item_1, R.id.layout_item_2, R.id.layout_item_3,
R.id.layout_item_4, R.id.layout_item_5, R.id.layout_item_6};
int[] menuItemTextViewResId = {R.id.menu_item_text_1, R.id.menu_item_text_2, R.id.menu_item_text_3,
R.id.menu_item_text_4, R.id.menu_item_text_5, R.id.menu_item_text_6};
if(null == menuWindow){
final View layout = View.inflate(this, R.layout.launcher_menu, null);
menuWindow = new PopupWindow(layout,LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
for(int i=0; i<6; i++){
...//处理菜单项点击事件,菜单图标、文字等
//菜单显示后,焦点移动到了PopupWindow上的菜单项,需要在菜单项捕获Menu键以达到隐藏菜单的效果
LinearLayout menuItemLayout = (LinearLayout)layout.findViewById(menuItemLayoutResId[i]);
menuItemLayout.setOnKeyListener(new OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
if(event.getAction() == KeyEvent.ACTION_DOWN){
if (menuWindow != null && menuWindow.isShowing()) {
menuWindow.dismiss();
}
}
break;
}
return false;
}
});
}
layout.setFocusable(true);
layout.setFocusableInTouchMode(true);
layout.setOnKeyListener(new OnKeyListener(){
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
if(event.getAction() == KeyEvent.ACTION_DOWN){
if (menuWindow != null && menuWindow.isShowing()) {
menuWindow.dismiss();
}
}
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
layout.findViewById(R.id.layout_item_1).requestFocus();
break;
}
return false;
}
});
}
if(!menuWindow.isShowing()){
//处理菜单项"添加"是否可用
mMenuAddInfo = mWorkspace.findAllVacantCellsFromModel();
boolean isAddEnabled = mMenuAddInfo != null && mMenuAddInfo.valid;
LinearLayout layout_item_add = (LinearLayout)menuWindow.getContentView().findViewById(R.id.layout_item_1);
layout_item_add.requestFocus();
layout_item_add.setClickable(isAddEnabled);
layout_item_add.setBackgroundResource(isAddEnabled ? R.drawable.launcher_menu_selector : R.drawable.launcher_menu_disabled);
Drawable icon = ThemeParser.loadMenuIcon(0);
icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
icon.setAlpha(isAddEnabled ? 0xFF :(int)(0.2 * 0xFF));
TextView text_item_add = (TextView)menuWindow.getContentView().findViewById(R.id.menu_item_text_1);
text_item_add.setTextColor(isAddEnabled ? Color.parseColor("#000000") : R.color.gray);
text_item_add.setCompoundDrawables(null, icon, null, null);
boolean isOrientationLandscape = ScreenUtil.isOrientationLandscape(this);
if(isOrientationLandscape){
menuWindow.setHeight(ScreenUtil.dip2px(this, 67));
}else{
menuWindow.setHeight(ScreenUtil.dip2px(this, 134));
}
((LinearLayout)menuWindow.getContentView().findViewById(R.id.launcher_menu)).
setBackgroundDrawable(ThemeParser.loadThemeMenuBackground(isOrientationLandscape));
menuWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.transparent));

menuWindow.setFocusable(true);
menuWindow.setOutsideTouchable(true);
//设置菜单显示隐藏的动画
menuWindow.setAnimationStyle(R.style.PopupAnimation);
menuWindow.showAtLocation(findViewById(R.id.drag_layer), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
menuWindow.update();
}else{
menuWindow.dismiss();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值