菜单是我们做应用 的时候经常要用到的,像UC等都有,今天也做了个小Demo,用POPWindow实现的。弹出效果主要使用了translate和alpha样式实现
POP的主要代码:
private void showPop() { ViewGroup menuView = (ViewGroup) getLayoutInflater().inflate( R.layout.gridview_pop, null, true); menuGrid = (GridView) menuView.findViewById(R.id.gridview); menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array)); menuGrid.requestFocus(); menuGrid.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(MainActivity.this, menu_name_array[arg2], Toast.LENGTH_LONG).show(); popupWindow.dismiss(); } }); popupWindow = new PopupWindow(menuView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); popupWindow.setAnimationStyle(R.style.PopupAnimation); popupWindow.showAtLocation(textView, Gravity.BOTTOM, 0, Dp2Px(45)); popupWindow.update(); }其中showAtLocation这个方法比较关键:
第一个参数指定PopupWindow的锚点view,即依附在哪个view上
第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移多少像素。
效果图如下:
但是要求从menu的下面一层滑出,这个用popw我就没能实现,我是用一个View实现的,如果谁用popw实现了可以交流交流,不胜感激。
我用View实现的代码:
private void showView() { if (ll_menu.getVisibility() == View.GONE) { menuGrid.setAdapter(getMenuAdapter(menu_name_array, menu_image_array)); ll_menu.setVisibility(View.VISIBLE); Animation animation_in = AnimationUtils.loadAnimation( MainActivity.this, R.anim.push_bottom_in); ll_menu.startAnimation(animation_in); } else { Animation animation_out = AnimationUtils.loadAnimation( MainActivity.this, R.anim.push_bottom_out); animation_out.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { ll_menu.setVisibility(View.GONE); } }); ll_menu.startAnimation(animation_out); } }