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);
}
}
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);
}
}