【问题标题】:Changing position of the Dialog on screen android在屏幕android上更改对话框的位置
【发布时间】:2012-03-17 01:04:00
【问题描述】:

我在我的Activity中做了一个简单的AlertDialog

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

infoDialog.show();

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

我想知道,如何自定义对话框位置以使其显示在顶部操作栏下方? (无论如何要改变对话框的重力或其他东西吗?)以及如何根据我的代码来做到这一点??

【问题讨论】:

  • 如果您向我们展示您的 my_dialog xml 布局,我们或许可以帮助您进行更改。
  • @Ankit,你能把你的评论作为答案吗,因为我的问题在检查你的链接后得到了解决。

标签: android android-layout android-intent android-emulator android-widget


【解决方案1】:

我使用此代码在屏幕底部显示对话框:

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 使对话框的背景变暗。您应该能够更改重力参数以移动对话框

【讨论】:

  • 您好,谢谢。我将其重力设置为顶部,对话框在屏幕顶部,但它也覆盖了我的操作栏,我希望对话框在顶部但在操作栏下方...如何调整?
  • 您可以尝试使用wlp.xwlp.y 字段来明确设置对话框在屏幕上的位置。我自己没有尝试过,但它应该可以工作。
  • 调光的清除对我不起作用(尽管其余的工作就像一个魅力)。我在另一个地方找到了调光的解决方案,形式为:window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
  • @AleksG 有没有办法改变昏暗背景的颜色,保持 dailog 居中?我尝试将 windowIsFloating=false 设置为 dailog 样式。但是它将dailog对齐到顶部!
  • @Kshitij 如果您想将对话保持在中心,则不需要任何此代码 - 默认情况下,Android 中心对话。至于改变暗淡的颜色,这是由主题控制的。您可以通过在您的应用程序中包含相应的主题来覆盖它。不过我从来没有尝试过。
【解决方案2】:
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();
}

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

【讨论】:

  • 您好,谢谢。我将其重力设置为顶部,对话框在屏幕顶部,但它也覆盖了我的操作栏,我希望对话框在顶部但在操作栏下方...如何调整?
  • 您可以设置布局顶部的边距
  • 'FILL_PARENT' 已弃用。我在 API 21 上。Developer Reference
  • @fWd82 所以请改用“MATCH_PARENT”
【解决方案3】:

我从@gypsicoder 代码here 找到了这个代码sn-p

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位置值是从下到上。

【讨论】:

  • 您修改后忘记设置LayoutParams。
  • @HarshVardhan 参数在getAttributes()返回的对象内修改,这是窗口管理器实际使用的对象。
【解决方案4】:

BottomSheetDialog:

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

dialog.show();

【讨论】:

    【解决方案5】:

    对我来说,当我试图将我的对话框定位在它被选中的文本视图底部的某个位置时,这非常有效。

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

    【讨论】:

      【解决方案6】:

      只需将其添加到您的代码中:

      dialog.getWindow().setGravity(Gravity.BOTTOM);
      

      【讨论】:

        【解决方案7】:
        dialog.getWindow().getAttributes().gravity = Gravity.BOTTOM;
        

        【讨论】:

          【解决方案8】:

          我用这个方法

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

          【讨论】:

            【解决方案9】:

            使用bottomSheet:

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

            【讨论】:

              【解决方案10】:

              您可以使用此 sn-p 将 AlertDialog 放置在屏幕底部。

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

              【讨论】:

                【解决方案11】:

                科特林:

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

                【讨论】:

                  【解决方案12】:

                  对于我的对话框活动,我使用了这个:

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

                  【讨论】:

                    【解决方案13】:

                    在 kotlin 中设置 Dialog 重力的最简单方法:

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

                    【讨论】:

                      【解决方案14】:
                      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);
                          }
                      }
                      

                      【讨论】:

                        【解决方案15】:

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

                        private void showDialog(final String scanContent, final String currentTime, final String currentDate) { LayoutInflater linf = LayoutInflater.from(this); final View inflator = linf.inflate(R.layout.dialog_barcode_result_dialog, 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>
                        

                        【讨论】:

                          【解决方案16】:

                          在 Java 中

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

                          【讨论】:

                            【解决方案17】:

                            A.I.Shakil 建议的 Java 版 kotlin 代码:

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

                            【讨论】:

                              猜你喜欢
                              • 2011-03-02
                              • 1970-01-01
                              • 1970-01-01
                              • 2013-04-30
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              相关资源
                              最近更新 更多