关于PopupWindow代码片段

本文介绍如何使用Android中的PopupWindow来实现一个可定制的下拉菜单,包括初始化视图、设置适配器及响应点击事件等关键步骤,并详细解释了如何调整弹窗的位置和尺寸。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

private PopupWindow window;

private RelativeLayout rl_complete;

private void showPopowindowDialog() {

View popwindow = inflater.inflate(R.layout.dialog_sort, null);
ListView lv = (ListView) popwindow.findViewById(R.id.main_dialog_lv);
PopupWindowAdapter popupWindowAdapter = null;
if(popupWindowAdapter == null){
popupWindowAdapter = new PopupWindowAdapter();
}
lv.setAdapter(popupWindowAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
key = datas[arg2];
currentSortIndex = arg2;
window.dismiss();
sendRankInfoReq();
}
});


DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;


window = new PopupWindow(popwindow, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.title_function_bg)); //这句话必须有 
window.setAnimationStyle(android.R.style.Animation_Dialog);
window.update();
window.setFocusable(true);
   window.setTouchable(true); //设置PopupWindow可触摸  
   window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸  
int[] xy = new int[2];
rl_complete.getLocationOnScreen(xy);
window.showAtLocation(rl_complete, Gravity.RIGHT | Gravity.TOP,
rl_complete.getWidth()/5,xy[1]+rl_complete.getWidth());


}


@Override
public boolean onTouchEvent(MotionEvent event) {
if (window != null && window.isShowing()) {


window.dismiss();


window = null;


}
return super.onTouchEvent(event);
}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (window != null && window.isShowing()) {


window.dismiss();


window = null;


} else {
finish();
}
}


return false;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值