Android弹出窗口

1.PopupWindow实现:
private void showPopupWindow(int x, int y, int width, int height) {
TextView textView = new TextView(this);
textView.setText("Hello popupWindow");
textView.setBackgroundColor(Color.CYAN);
PopupWindow popupWindow = new PopupWindow(textView, width, height);
popupWindow.showAtLocation(getWindow().getDecorView(),
Gravity.NO_GRAVITY, x, y);
}
不能在onCreate中,否则会报错,需要View渲染完成后调用

2.Dialog实现
public class MyActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//在指定位置显示对话框
showDialog(100, 100, 200, 200);
}

@Override
protected Dialog onCreateDialog(int id, Bundle args) {
return new AlertDialog.Builder(this)
.setTitle("Hello Dialog!")
.setMessage("Hello Dialog")
.create();
}

@Override
protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
switch (id) {
case 1: {
//设置对话框的属性
Window window = dialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.x = args.getInt("x");
lp.y = args.getInt("y");
lp.width = args.getInt("width");
lp.height = args.getInt("height");

lp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(0, 0, 0, 0)));
}
break;

default:
break;
}

super.onPrepareDialog(id, dialog, args);
}

private void showDialog(int x, int y, int width, int height) {
Bundle bundle = new Bundle();
bundle.putInt("x", x);
bundle.putInt("y", y);
bundle.putInt("width", width);
bundle.putInt("height", height);

showDialog(1, bundle);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值