Android弹出窗口
16lz
2022-07-27
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);
}
}
更多相关文章
- 浅谈Java中Collections.sort对List排序的两种方法
- python list.sort()根据多个关键字排序的方法实现
- android EditText设置不可写
- Android(安卓)拨号器的简单实现
- android实现字体闪烁动画的方法
- Android中不同应用间实现SharedPreferences数据共享
- [Android(安卓)NDK]Android(安卓)JNI开发例子 ---3 在JNI中实现o
- Andorid Dialog 示例【慢慢更新】
- android 拨打紧急号码,通话时开启免提功能实现