Lee*_*fin 96 android android-widget android-emulator android-intent android-layout
我AlertDialog
在我的活动中做了一个简单的事:
View view = layoutInflater.inflate(R.layout.my_dialog, null);
AlertDialog infoDialog = new AlertDialog.Builder(MyActivity.this)
.setView(view)
.create();
infoDialog.show();
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,对话框显示在屏幕的中心(大约).
我想知道,如何自定义对话框位置以使其显示在顶部操作栏下?(无论如何改变对话的重力或某些东西?)以及如何根据我的代码做到这一点?
Ale*_*s G 213
我使用此代码显示屏幕底部的对话框:
Dialog dlg = <code to create custom dialog>;
Window window = dlg.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
Run Code Online (Sandbox Code Playgroud)
如果需要,此代码还可以防止android调暗对话框的背景.您应该能够更改重力参数以移动对话框
Ram*_*nki 23
private void showPictureialog() {
final Dialog dialog = new Dialog(this,
android.R.style.Theme_Translucent_NoTitleBar);
// Setting dialogview
Window window = dialog.getWindow();
window.setGravity(Gravity.CENTER);
window.setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
dialog.setTitle(null);
dialog.setContentView(R.layout.selectpic_dialog);
dialog.setCancelable(true);
dialog.show();
}
Run Code Online (Sandbox Code Playgroud)
您可以根据重力和布局参数自定义对话框,根据您的要求更改重力和布局参数
对我来说,这很好用,我试图将对话框放在textview底部的某个位置,在那里它被选中.
public void setPosition(int yValue) {
Window window = getWindow();
WindowManager.LayoutParams param = window.getAttributes();
param.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
param.y = yValue;
window.setAttributes(param);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
Run Code Online (Sandbox Code Playgroud)
我在这里找到了@gypsicoder代码的代码片段
private CharSequence[] items = {"Set as Ringtone", "Set as Alarm"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if(item == 0) {
} else if(item == 1) {
} else if(item == 2) {
}
}
});
AlertDialog dialog = builder.create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes();
wmlp.gravity = Gravity.TOP | Gravity.LEFT;
wmlp.x = 100; //x position
wmlp.y = 100; //y position
dialog.show();
Run Code Online (Sandbox Code Playgroud)
这里x位置的值是从左到右的像素.对于y位置值是从下到上.
小智 8
新BottomSheetDialog
:
BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
dialog.setContentView(YourView);
dialog.show();
Run Code Online (Sandbox Code Playgroud)
小智 5
只需将其添加到您的代码中:
dialog.getWindow().setGravity(Gravity.BOTTOM);
Run Code Online (Sandbox Code Playgroud)