PopupWindow弹出窗体可以在任意位置弹出窗体,而对话框只能出现屏幕最中间
showAsDropDown(View anchor, int xoff, int yoff) 可以设置显示的位置 相对于兄弟控件
showAtLocation(View parent, int gravity, int x, int y) 相对于父控件
public PopupWindow (Context context):context上下文对象
setContentView():设置自定义布局
setWidth():设置宽度
setHeight():设置高度
必须要设置的3个属性
可以勇敢布局填充其来显示其他的页面布局 调用其它布局的方法
opupWindow.setOutsideTouchable(true);
popupWindow.setFocusable(false);
设置按钮的焦点
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha=0.3f;
getWindow().setAttributes(attributes);
窗体的管理
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha=1.0f;
getWindow().setAttributes(attributes);
}
});
当PopopWindow关闭时的方法
菜单
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(1,Red,4,“红色”).setIcon(R.mipmap.ic_launcher);
menu.add(1,Yellow,2,“黄色”).setIcon(R.mipmap.ic_launcher);
menu.add(1,Green,1,“绿色”).setIcon(R.mipmap.ic_launcher);
menu.add(1,White,3,“白色”).setIcon(R.mipmap.ic_launcher);
menu.add(1,Pouple,5,“蓝色”).setIcon(R.mipmap.ic_launcher);
Log.i(“MenuActivity”,“FAFSA”);
// SubMenu subMenu=menu.addSubMenu(“jh”);
// subMenu.add(2,)
return true;
}
重写方法 onCreateOptionsMenu
参数依次为 组号 id 顺序 文本 也可以设置图片 低版本不会显示
retrun turn 才会显示
重写方法onOptionsItemSelected 做条目的监听事件