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

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. python list.sort()根据多个关键字排序的方法实现
  3. android EditText设置不可写
  4. Android(安卓)拨号器的简单实现
  5. android实现字体闪烁动画的方法
  6. Android中不同应用间实现SharedPreferences数据共享
  7. [Android(安卓)NDK]Android(安卓)JNI开发例子 ---3 在JNI中实现o
  8. Andorid Dialog 示例【慢慢更新】
  9. android 拨打紧急号码,通话时开启免提功能实现

随机推荐

  1. 面向忙碌开发者的 Android(安卓)知识点收
  2. Smalidea+IntelliJ IDEA/Android(安卓)St
  3. Android的listview、gridview 笔记
  4. Android(安卓)混淆提示 java.io.FileNotF
  5. Android(安卓)market 发布个人免费应用的
  6. Android深入浅出系列课程---Lesson0 SHW1
  7. Mac下配置android环境变量
  8. android 报错java.io.IOException: Permi
  9. Android联系人数据库全解析(4)
  10. android 拨号盘Contact模块讲解(四)