【问题标题】:Android custom AlertDialog changed width not accurateAndroid自定义AlertDialog改变宽度不准确
【发布时间】:2014-08-13 04:39:33
【问题描述】:

我有一个DialogFragment,我在 onCreate() 中创建了alertDialog

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    if (alertDialog == null) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        alertDialog = builder.create();
        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
        alertDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    }
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    alertDialog.setView(getDialogLayout(),0,0,0,0);
    return alertDialog;
}

然后我在onStart()中设置alertDialog的宽度(dialogWidth):

    @Override
public void onStart() {
        super.onStart();
        WindowManager.LayoutParams lp = alertDialog.getWindow().getAttributes();
        lp.width = dialogWidth; 
        lp.x = Constants.iX_PositionDialog;
        lp.y = Constants.iY_PositionDialog;
        alertDialog.getWindow().setAttributes(lp);
    }

在我的例子中,我将对话框的宽度设置为 648,但我的 surfaceView 的画布/窗口只有 590,为什么? 我需要我设置的宽度。

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    我为自己找到了解决方案。

    所以 alertDialog 视图被打包在几个 FrameLayouts 中。

    其中一些的 Padding 不是 0。

    根据这里的帮助代码:[AlertDialog with custom view: Resize to wrap the view's content

    我使用以下方法实现它,我在 onStart 方法中调用它:

    protected void forceWrapContent(View v) {
        // Start with the provided view
        View current = v;
    
        // Travel up the tree until fail, modifying the LayoutParams
        do {
            // Get the parent
            ViewParent parent = current.getParent();    
    
            // Check if the parent exists
            if (parent != null) {
                // Get the view
                try {
                    current = (View) parent;
                } catch (ClassCastException e) {
                    // This will happen when at the top view, it cannot be cast to a View
                    break;
                }
    
                // Modify the layout
                current.getLayoutParams().width = dialogWidth;
                current.setPadding(0, 0, 0, 0);
    
            }
        } while (current.getParent() != null);
    
        // Request a layout to be re-done
        current.requestLayout();
    }
    

    谢谢!

    【讨论】:

      【解决方案2】:

      设置警报对话框的宽度和高度以供不同屏幕使用:

      int dialogWidth = getActivity().getResources().getDisplayMetrics().widthPixels-120; // screen width - whatever the width you want to set.
      int dialogHeight = getActivity().getResources().getDisplayMetrics().heightPixels -140; //screen height - whatever the width you want to set.
      getDialog().setCanceledOnTouchOutside(false);
      Window window = getDialog().getWindow();
      WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
      lp.gravity = Gravity.CENTER;
      lp.height = dialogHeight;
      lp.width = dialogWidth;
      window.setAttributes(lp);
      

      这是一个最佳实践,因为在安卓有很多设备和很多分辨率,所以我们必须根据不同的屏幕来做所有事情。这样它对所有类型的屏幕都是可行的。

      【讨论】:

        【解决方案3】:

        alertDialogshow()方法之后设置布局。

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(layout);
        builder.setTitle("Title");
        alertDialog = builder.create();
        alertDialog.show();
        alertDialog.getWindow().setLayout(648, 400); //Controlling width and height.
        

        注意:在show()之后设置布局是重点。

        更多信息 - how-to-control-the-width-and-height-of-default-alert-dialog-in-android.

        【讨论】:

        • 不幸的是它不起作用。我可以按我的方式设置宽度,但我的问题是:为什么宽度不准确。
        猜你喜欢
        • 2012-10-29
        • 1970-01-01
        • 2012-05-08
        • 1970-01-01
        • 1970-01-01
        • 2011-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多