PopupWindow的使用

时间:2022-03-17 02:38:32

   

2种常用PopupWindow的使用

下载地址:

第一个展示一个下拉的ListView

/** * 展示第一个弹窗 */ private ListView mListView; private String[] itemStrings = {"第一个人啊","第二个人啊","第三个人啊","第四个人啊","第五个人啊","第六个人啊","第七个人啊"}; protected void setOnePoP() { // TODO Auto-generated method stub View contentView = View.inflate(this, R.layout.pop_list, null); if(popup_one == null){ popup_one = new PopupWindow(contentView,btn_one.getWidth(),LayoutParams.WRAP_CONTENT,true); } mListView = (ListView)contentView.findViewById(R.id.lv_pop_listview); mListView.setAdapter(new MyAdapter(MainActivity.this, itemStrings)); popup_one.setFocusable(true); popup_one.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popup_one.setOutsideTouchable(true); popup_one.showAsDropDown(btn_one); } /** * ListView的适配器 * @author Administrator * */ private class MyAdapter extends BaseAdapter{ private Context mContext; private String [] item ; public MyAdapter(Context mContext, String[] item) { this.mContext = mContext; this.item = item; } @Override public int getCount() { // TODO Auto-generated method stub return item.length; } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub ViewHolder holder = null; if(arg1 == null){ holder = new ViewHolder(); arg1 = View.inflate(mContext, R.layout.item_list, null); holder.tv_name = (TextView)arg1.findViewById(R.id.item_tv_formname); arg1.setTag(holder); }else { holder = (ViewHolder)arg1.getTag(); } holder.tv_name.setText(item[arg0] + ""); return arg1; } } static class ViewHolder{ private TextView tv_name; }

第二个展示在下方展示一个弹窗,并设置屏幕透明度屏幕变暗的效果

代码如下

/** * 在底部展示一个弹窗,,并把界面的颜色变暗 */ protected void setTwoPoP() { View view = View.inflate(MainActivity.this, R.layout.pop_two, null); if(popup_two == null){ popup_two = new PopupWindow(view,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,true); } Button btn_dis = (Button)view.findViewById(R.id.btn_dis); btn_dis.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(popup_two !=null && popup_two.isShowing()){ popup_two.dismiss(); backgroundAlpha(1f); } } }); popup_two.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); //展示一个动画 popup_two.setAnimationStyle(R.style.popWindow_anim_style); popup_two.setFocusable(true); popup_two.setOutsideTouchable(true); backgroundAlpha(0.5f); popup_two.showAtLocation(MainActivity.this.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0); popup_two.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { // TODO Auto-generated method stub backgroundAlpha(1f); } }); } /** * 设置添加屏幕的背景透明度 * @param bgAlpha */ public void backgroundAlpha(float bgAlpha) { this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); WindowManager.LayoutParams lp = this.getWindow().getAttributes(); lp.alpha = bgAlpha; this.getWindow().setAttributes(lp); }

第三对于第2中效过,用Dialog也可以实现,需要设置Dialog在屏幕的位置