AlertDialog
是一个用于在 Android 应用程序中显示对话框的类
custom_alert_dialog.xml
。在这个布局文件中,你可以设置对话框的大小、位置和样式。<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Add your custom views here -->
</LinearLayout>
AlertDialog.Builder
类创建一个 AlertDialog
实例,并将自定义布局文件设置为对话框的内容视图。AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(R.layout.custom_alert_dialog);
AlertDialog alertDialog = builder.create();
WindowManager.LayoutParams
类来设置对话框的位置。alertDialog.show();
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(alertDialog.getWindow().getAttributes());
// Set the desired position (x, y) and gravity
layoutParams.x = 100; // X position in pixels
layoutParams.y = 200; // Y position in pixels
layoutParams.gravity = Gravity.TOP | Gravity.START;
alertDialog.getWindow().setAttributes(layoutParams);
通过这种方法,你可以自由地调整 AlertDialog
的显示位置。请注意,这里的位置值是以像素为单位的,你可能需要根据屏幕密度进行转换。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:Android中PopupMenu的显示位置如何调整