android 模态透明窗体,android 中弹出透明模态框

此文章有本人亲撰,但是下面所提到的code 并非本人所写写这篇日志的目的在于记录这个知识点防止以后忘记同时也希望能帮助可能用到这种效果的人!

需求 就是通过 点击一个button 弹出来 类似于Dialog这样的效果

这里我们可以通过两种方式实现

第一种:就是通过dialog

第二种:通过PopupWindow

第一种如下重要的伪代码如下:

我们需要通过 dialog 弹出来一个 listview

private void openDialog() {

View menuView = View.inflate(this, R.layout.gridview_menu, null);

// 创建AlertDialog

final AlertDialog menuDialog = new AlertDialog.Builder(this).create();

menuDialog.setView(menuView);

menuGrid = (GridView) menuView.findViewById(R.id.gridview);

menuGrid.setAdapter(getMenuAdapter(menu_name_array,menu_image_array));

menuGrid.setOnItemClickListener(new OnItemClickListener() {

@Override

public void onItemClick(AdapterView> arg0, View arg1, int arg2,long arg3) {

if (arg2 == 11) {

menuDialog.cancel();

}

}

});

menuDialog.show();

}

首先拿到 你要显示的listview 组件

然后我们通过设置dialog的view 来显示 显示出来之后 至于 你要在listview 中得item 中 实现什么功能那就要自己 添加了

dialog这一种方式 只是 通过 一个自定义的view 来填充 dialog 本身包含的view(我个人理解是这样的。。)

第二种方式:

LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

ViewGroup menuView = (ViewGroup) mLayoutInflater.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) {

if (arg2 == 11) {

popupWindow.dismiss();

}

}

});

menuGrid.setOnKeyListener(new OnKeyListener() {// 焦点到了gridview上,所以需要监听此处的键盘事件。否则会出现不响应键盘事件的情况

@Override

public boolean onKey(View v, int keyCode, KeyEvent event) {

switch (keyCode) {

case KeyEvent.KEYCODE_MENU:

if (popupWindow != null && popupWindow.isShowing()) {

popupWindow.dismiss();

}

break;

}

System.out.println("menuGridfdsfdsfdfd");

return true;

}

});

popupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT,

LayoutParams.FILL_PARENT, true);

popupWindow.setBackgroundDrawable(new BitmapDrawable());

popupWindow.setAnimationStyle(R.style.PopupAnimation);

popupWindow.showAtLocation(findViewById(R.id.parent), Gravity.CENTER

| Gravity.CENTER, 0, 0);

popupWindow.update();

个人理解 就是 LayoutInflater (布局填充者) 获取到我们事先定义好的布局界面

PopupWindow(menuView, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, true);

定义弹出来的 组件填充全屏

popupWindow.setBackgroundDrawable(new BitmapDrawable());

这个地方一定要设置 而且一定要设置在

popupWindow.showAtLocation(findViewById(R.id.parent), Gravity.CENTER

| Gravity.CENTER, 0, 0);

之前

我猜想是这样的。。如果popupWindow 没有 画好背景的话他是不会弹出来的,(他必须把需要的东西准备好才会弹出来) ..

至于findViewById(R.id.parent),  这个东西就是你要弹出在那个view之上的那个view Id

。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在JSP中点击按钮弹模态框,可以使用Bootstrap框架中的Modal组件来实现。 首先,在JSP页面中引入Bootstrap的CSS和JS文件: ```html <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script> ``` 然后,在页面中添加一个按钮: ```html <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">打开模态框</button> ``` 其中,`data-toggle="modal"`表示点击按钮时弹模态框,`data-target="#exampleModal"`表示模态框的ID为exampleModal。 最后,在页面中添加模态框的HTML代码: ```html <div class="modal" tabindex="-1" role="dialog" id="exampleModal"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">模态框标题</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <p>模态框内容</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">关闭</button> <button type="button" class="btn btn-primary">保存</button> </div> </div> </div> </div> ``` 在上述代码中,`id="exampleModal"`与按钮中的`data-target="#exampleModal"`对应,`class="modal"`表示这是一个模态框,`role="dialog"`表示角色为对话框。 点击按钮后,模态框就会弹,点击关闭按钮或者点击模态框外部区域都可以关闭模态框

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值