Android使用PopupWindow创建一个简单的菜单
PopupWindow是一个弹出式窗口,它可以展示任意View。他会浮在当前窗口的上方展示。
下面看代码:
public class MyActivity extends Activity |
{ |
private PopupWindow menu; |
private LayoutInflater inflater; |
private View layout; |
|
@Override |
public void onCreate(Bundle savedInstanceState) |
{ |
super .onCreate(savedInstanceState); |
setContentView(R.layout.main); |
//实例化PopupWindow创建菜单 |
initMenu(); |
} |
|
//判断按键 菜单的显示与隐藏 |
@Override |
public boolean onKeyDown( int keyCode, KeyEvent event) { |
if (!menu.isShowing()&&keyCode == KeyEvent.KEYCODE_MENU){ |
show(); |
} else { |
menu.dismiss(); |
} |
if (keyCode == KeyEvent.KEYCODE_BACK&&menu.isShowing()){ |
menu.dismiss(); |
} |
return true ; |
} |
//实例化PopupWindow创建菜单 |
private void initMenu(){ |
|
//获取LayoutInflater实例 |
inflater = (LayoutInflater) this .getSystemService(LAYOUT_INFLATER_SERVICE); |
//获取弹出菜单的布局 |
layout = inflater.inflate(R.layout.menu, null ); |
//设置popupWindow的布局 |
menu = new PopupWindow(layout, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT); |
|
} |
|
//显示菜单 |
private void show(){ |
//设置位置 |
menu.showAtLocation( this .findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0 , 0 ); //设置在屏幕中的显示位置 |
} |
} |
单的布局文件:
<? xml version = "1.0" encoding = "utf-8" ?> |
< LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
android:layout_width = "fill_parent" |
android:layout_height = "fill_parent" |
android:orientation = "horizontal" |
> |
< ImageView |
android:id = "@+id/icon_1" |
android:background = "@drawable/icon_1" |
android:layout_width = "40dp" |
android:layout_height = "40dp" /> |
< ImageView |
android:id = "@+id/icon_2" |
android:background = "@drawable/icon_2" |
android:layout_width = "40dp" |
android:layout_height = "40dp" /> |
< ImageView |
android:id = "@+id/icon_3" |
android:background = "@drawable/icon3" |
android:layout_width = "40dp" |
android:layout_height = "40dp" /> |
< ImageView |
android:id = "@+id/icon_4" |
android:background = "@drawable/icon4" |
android:layout_width = "40dp" |
android:layout_height = "40dp" /> |
</ LinearLayout > |
半透明弹出框PopUpWindow
http://www.apkbus.com/android-2474-1-1.html
PopUpWindow实现半透明弹出框关键点:
布局文件 最外层设置为全屏 背景颜色为半透明
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/translucent"
android:gravity="center"
android:orientation="vertical"
>
.......
<ListView
android:id="@+id/qianghaoqi_diary_pop_list"
android:divider="@null"
android:scrollingCache="false"
android:fadingEdge="none"
android:scrollbarThumbVertical="@drawable/game_blade_qianghaoqi_listview_scrollbar"
android:layout_marginBottom="@dimen/dip5"
android:layout_marginLeft="@dimen/dip5"
android:layout_marginRight="@dimen/dip10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:listSelector="@null"
android:paddingLeft="15.0dip"
/>
.........
</RelativeLayout>
new一个全屏的PopUpWindow
//必须为true,可以获取焦点
mPopWin = new PopupWindow(aPopView, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,true);
//mPopWin.setHeight(450);//设置PopupWindow高度
//必须设置。改变弹出窗口的背景,当然也可以设置为NULL。
mPopWin.setBackgroundDrawable(mActivity.getResources().getDrawable(R.drawable.game_blade_qianghaoqi_transparent_bg));
mPopWin.showAtLocation(aParentView,Gravity.CENTER, 0, 0);
//如果窗口已经显示过,更改此值只能在下一次显示时起作用,或者调用update()
mPopWin.update();
如果PopUpWindow内的布局会夺取焦点(如示例ListView),注意代码
mListView.requestFocus();
// 焦点到了listView上,所以需要监听此处的键盘事件。否则会出现不响应键盘事件的情况
mListView.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
closePopWin();
}
return true;
}
});
关闭PopUpWindow
private void closePopWin(){
if (mPopWin != null && mPopWin.isShowing()) {
mPopWin.dismiss();
}
}
PopUpWindow一般应用
mPopWin = new PopupWindow(mPopView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
mPopWin.setBackgroundDrawable(mActivity.getResources().getDrawable(R.drawable.game_blade_qianghaoqi_transparent_bg));
mPopWin.setOutsideTouchable(true);
mPopWin.setFocusable(true);
mPopWin.showAsDropDown(aParentView,0,-15);
mPopWin.update();
Android之PopWindow
1.设置半透明主题
2.设置window的alpha值
弹出popwindow的代码如下,比较乱,多包涵:
popupWindow = new PopupWindow(menuView,LayoutParams.FILL_PARENT
LayoutParams.FILL_PARENT,true);
popupWindow.showAtLocation(findViewById(R.id.parent),Gravity.CENTER
|Gravity.CENTER, 0, 0);
popupWindow.setAnimationStyle(R.style.PopupAnimation);
//加上下面两行可以用back键关闭popupwindow,否则必须调用dismiss();
ColorDrawable dw = new ColorDrawable(-00000);
popupWindow.setBackgroundDrawable(dw);
popupWindow.update();
<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="fill_parent"
android:gravity="center"android:layout_height="fill_parent"
android:layout_gravity="center"android:background="#b0000000" >
<LinearLayoutandroid:orientation="vertical"
android:layout_width="wrap_content"android:gravity="center"
android:layout_height="wrap_content"android:layout_gravity="center"
android:background="@drawable/downbutton_corner">
<GridViewandroid:id="@+id/gridview"android:layout_width="wrap_content"
android:layout_height="wrap_content"android:numColumns="4"
android:verticalSpacing="5dip"android:horizontalSpacing="5dip"
android:stretchMode="columnWidth"android:gravity="center"
android:layout_gravity="center"/></LinearLayout></LinearLayout>
2。popupwindow的边框,圆角背景。downbutton_corne.xml
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradientandroid:startColor="#c0000000"android:endColor="#c0000000"
android:angle="90" /><!--背景颜色渐变-->
<strokeandroid:dashWidth="2dp" android:dashGap="2dp"
android:width="2dp"android:color="#FF00ff00"></stroke>
<!--描边-->
<cornersandroid:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"android:topLeftRadius="5dp"
android:topRightRadius="5dp"/><!--设置圆角-->
</shape>
这条代码是设置style的,动画文件就是在style文件里面引入的。下面是淡入的动画,动画教程网上也很多。淡出的动画就这些参数值交换位置就是了。android:duration这个是持续时间,为了截图,我把它弄成5秒了。
<setxmlns:android="http://schemas.android.com/apk/res/android">
<scaleandroid:fromXScale="0.6" android:toXScale="1.0"
android:fromYScale="0.6" android:toYScale="1.0"android:pivotX="50%"
android:pivotY="50%" android:duration="5000"/>
<alphaandroid:interpolator="@android:anim/decelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"android:duration="5000" />
</set>
在gridview加上setOnKeyListener,就能解决。
menuGrid.setOnKeyListener(newOnKeyListener() {
@Override
publicboolean onKey(View v, int keyCode, KeyEvent event){
switch(keyCode) {
caseKeyEvent.KEYCODE_MENU:
if(popupWindow != null &&popupwindows.isShowing()) {
popupWindow.dismiss();
System.out.println("menuGridfdsfdsfdfd");
}
break;
}
returntrue;
}
});
---------------------------------------------------------------------------------
使用PopupWindow来做自定义menu,往PopupWindow增加一个子View,子View的布局就是menu的布局。
出现和退出的动画:可以给PopUpWindow或它的子view添加。
网上所有用PopupWindow做的menu有个共同特点:就是点击menu键出现PopupWindow,然后再点击menu键无法使PopupWindow退出/dismiss()。
当给PopupWindow设置了setFocusable(true),menu显示后,点击menu其他任何地方,menu都会消失,但是这时候按钮的点击事件其实是不响应的。同时只响应键盘的返回键,其他按键均不响应,比如点击menu键,没有任何反应。
要解决这个问题很简单,就是给PopupWindow的子View设置下面的代码:
记住,一定要给PopupWindow设置setFocusable(true),要不然点击menu其他地方以及返回键,menu都不会退出。且这时候是响应PopupWindow的parent的menu事件的。
下面阐述为什么这么写之后,当PopupWindow显示后,点击menu键PopupWindow会退出的原因:
首先得明白为什么给PopupWindow setFocusable(true)后,点击menu出现PopupWindow后再点击menu没反应的原因。
PopupWindow初始化的时候一般都指定了在哪个View上出现,我们称这个View为parent。parent里面写了点击menu出现PopupWindow的事件,如果给PopupWindow setFocusable(true),此时屏幕的焦点在PopupWindow上面,肯定是不会响应parent的按键事件的,它只会响应PopupWindow的按键事件。
但是PopupWindow的本质是Window,没有继承View类,自己没有onkeyDown或onkey或dispatchKey这些事件的。我刚开始试着实现这些接口,但是按键依然不响应,不知原因。因现在对按键的原理还不熟,无法阐述其原因。
然后我想绕道而行,就是给PopupWindow的子View注册按键事件,setKeyListener,刚开始我在子View的xml设置了android:focusable=”true” 但按键事件依然不响应。。。。纠结啊纠结。。。然后没得办法,我google了所有关于PopupWindow的文章。。。最后终于被我发现。。。需要给PopupWindow的子View 设置setFocusableInTouchMode(true)。这时候按键事件就响应了。。。
下面附上完整代码: