改变屏幕上对话框的位置

我在我的 活动里做了一个简单的 AlertDialog:

View view = layoutInflater.inflate(R.layout.my_dialog, null);
AlertDialog infoDialog = new AlertDialog.Builder(MyActivity.this)
.setView(view)
.create();


infoDialog.show();

使用上面的代码,对话显示在屏幕的(大约)中心。

我想知道,我怎样才能自定义对话框的位置,使它显示在顶部的动作栏?(有没有什么方法可以改变对话的重力或者其他什么东西?)以及如何根据我的代码做到这一点? ?

163273 次浏览
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();
}

您可以根据重力和布局参数自定义对话框 根据需要改变重力和布局参数

我使用这段代码来显示屏幕底部的对话框:

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

这段代码还可以防止 android 调暗对话框的背景(如果需要的话)。你应该能够改变重力参数来移动对话框

对于我来说,在我试图将对话框放置在文本视图底部的某个位置时,这个方法非常有效。

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

我发现了这个代码片段来自于这个吉普赛人代码 给你

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

这里 x 位置的值是从左到右的像素值,而 y 位置的值是从底部到顶部的像素值。

dialog.getWindow().getAttributes().gravity = Gravity.BOTTOM;

新的 BottomSheetDialog:

BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
dialog.setContentView(YourView);


dialog.show();

把这个加到你的代码里:

dialog.getWindow().setGravity(Gravity.BOTTOM);
public class MyDialogFragment extends DialogFragment{
protected void setDialogGravity(int gravity) {
Dialog dialog = getDialog();
if (dialog != null) {
Window window = dialog.getWindow();
if (window != null) {
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
params.horizontalMargin = 0;
params.gravity = gravity;
params.dimAmount = 0;
params.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(params);
}
}
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater,container,savedInstanceState);


return inflater.inflate(R.layout.my_dialog, null);
}


@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setDialogGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
}
}

在我的对话活动中,我使用了这个:

WindowManager.LayoutParams lp = this.getWindow().getAttributes();
lp.gravity = Gravity.BOTTOM;

我用这种方法

 @Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
dialog.setContentView(R.layout.dialog_droppoint);
dialog.show();
window.setAttributes(wlp);
return dialog;


}

使用底纸:

BottomSheetDialog dialog = new BottomSheetDialog(YourActivity.this);
dialog.setContentView(YourView);
dialog.show();

您可以使用此代码片段将 AlertDialog 放置在屏幕底部。

AlertDialog dialogPopup;


dialogPopup = mBuilder.create();
dialogPopup.getWindow().getAttributes().gravity = Gravity.BOTTOM;

我用自定义布局编写了一个自定义对话框。 它有一个 cancel和一个 save按钮,你也可以设置设备的屏幕上的重力(底部) ,并定义宽度和高度的对话框。

Private void showDialog (final String ScanContent,final String currentTime,final String currentDate){ 从(this) ; Final View flator = linf.flate (R.layout.Dialogue _ barcode _ result _ Dialogue,null) ;

final Dialog dialog = new Dialog(this, android.R.style.Theme_DeviceDefault_Light_Dialog);


// Setting dialogview
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);


dialog.getWindow().setLayout(375, 350);


window.setLayout(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);
dialog.setTitle(null);
dialog.setContentView(R.layout.dialog_barcode_result_dialog);


dialog.setTitle(getResources().getString(R.string.dialog_title));
dialog.setContentView(inflator);


final Button save = inflator.findViewById(R.id.saveBtn);
final Button cancel = inflator.findViewById(R.id.cancelBtn);
final TextView message = inflator.findViewById(R.id.dialog_message_text);
message.setText(scanContent);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();


}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();


}
});


dialog.show();

}

对话框布局 xml 文件是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:minWidth="350dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">


<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="vertical">


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:layout_marginBottom="10dp"
android:id="@+id/dialog_message_text"
/>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal">


<Button
android:id="@+id/cancelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel" />


<Button
android:id="@+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_save" />
</LinearLayout>


</LinearLayout>
</ScrollView>
</LinearLayout>

Kotlin:

val dialog = Dialog(context)
//set graviy bottom
dialog.window.setGravity(Gravity.BOTTOM)

用爪哇语

AlertDialog.Builder builder = new AlertDialog.Builder(ConnectActivity.this);


AlertDialog alertDialog = builder.create();
// set the gravity
alertDialog.getWindow().setGravity(Gravity.TOP);
// set the margin
alertDialog.getWindow().getAttributes().verticalMargin = 0.1F;
alertDialog.show();

在 Kotlin 中设置 对话重力最简单的方法是:

dialog.window?.setGravity(Gravity.TOP) // Set the gravity here
private fun searchDialog() {
val dialog = Dialog(this)
dialog.window?.setGravity(Gravity.TOP) // Set the gravity here
val binding = DialogSearchHomeBinding.inflate(layoutInflater)
dialog.setContentView(binding.root)
dialog.show()
}

A.I.Shakil 推荐的 kotlin 代码的 Java 版本:

 AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setView(v)
.setCancelable(false).create();
dialog.getWindow().setGravity(Gravity.TOP);
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();

为了在某个位置显示对话框,只需在 onViewCreated方法中添加这一行。

dialog?.window?.attributes?.gravity = Gravity.BOTTOM

当然你可以使用任何重力值。