带有自定义视图的 AlertDialog:调整大小以包裹视图的内容

2023-12-03

我在正在构建的应用程序中遇到了这个问题。请忽略所有设计缺陷和缺乏最佳实践方法,这纯粹是为了展示我无法解决的问题的示例。

I have DialogFragment它返回一个基本的AlertDialog与定制View设置使用AlertDialog.Builder.setView()。如果这View有特定尺寸要求,我如何获得Dialog正确调整自身大小以显示自定义中的所有内容View?

这是我一直在使用的示例代码:

package com.test.test;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Use a button for launching
        Button b = new Button(this);
        b.setText("Launch");
        b.setOnClickListener(new OnClickListener() {    
            @Override
            public void onClick(View v) {
                // Launch the dialog
                myDialog d = new myDialog();
                d.show(getFragmentManager(), null);
            }
        });

        setContentView(b);
    }

    public static class myDialog extends DialogFragment {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {           
            // Create the dialog
            AlertDialog.Builder db = new AlertDialog.Builder(getActivity());
            db.setTitle("Test Alert Dialog:");
            db.setView(new myView(getActivity()));

            return db.create();
        }

        protected class myView extends View {
            Paint p = null;

            public myView(Context ct) {
                super(ct);

                // Setup paint for the drawing
                p = new Paint();
                p.setColor(Color.MAGENTA);
                p.setStyle(Style.STROKE);
                p.setStrokeWidth(10);
            }

            @Override
            protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
                setMeasuredDimension(800, 300);
            }

            @Override
            protected void onDraw(Canvas canvas) {
                // Draw a rectangle showing the bounds of the view
                canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), p);
            }
        }
    }
}

A Button已创建,这将打开DialogFragment单击一下。习俗View (myView) 需要具有 800 的宽度和 300 的高度,这是在覆盖中正确设置的onMeasure(). This View,以洋红色绘制其测量边界以用于调试目的。

800 宽度比默认宽度更宽Dialog我的设备上的尺寸,但被剪裁而不是正确拉伸。

我查看了以下解决方案:

  • DialogFragment.getDialog 返回 null
  • 如何控制Android中默认Alert Dialog的宽度和高度?
  • 警报对话框或自定义警报对话框的大小

我推导出以下两种编码方法:

  1. Get the WindowManager.LayoutParams of the Dialog并使用覆盖它们myDialog.getDialog().getWindow().get/setAttributes()
  2. 使用setLayout(w, h)方法通过myDialog.getDialog().getWindow().setLayout()

我已经在我能想到的所有地方都尝试过(覆盖onStart(), in a onShowListener, 之后Dialog创建并显示等)并且通常可以使两种方法正常工作,如果LayoutParams提供一个具体值。但每当WRAP_CONTENT已提供,没有任何反应。

有什么建议么?

EDIT:

Screenshot of the situation: enter image description here

Screenshot of a specific value (note 900 is entered here, 850 doesn't cover the entire width of the View, which makes sense given the entire window is being adjusted. So that provides - if another was needed - reason why WRAP_CONTENT is essential / fixed values are not appropriate): enter image description here


我有一个可行的解决方案,说实话,我认为挖掘得太深,无法获得如此简单的结果。但这里是:

究竟发生了什么:

By opening the Dialog layout with the Hierarchy Viewer, I was able to examine the entire layout of the AlertDialog and what exactly what was going on: enter image description here

The blue亮点是所有高级部分(Window,框架为Dialog视觉风格等)以及从结尾开始blue下面是组件的位置AlertDialog are (red= 标题,yellow= 一个滚动视图存根,可能用于列表AlertDialogs, green = Dialog内容即自定义视图,orange= 按钮)。

从这里可以清楚地看出,7 视图路径(从blue到最后green)是什么未能正确WRAP_CONTENT。看着LayoutParams.width每个View透露一切都已给予LayoutParams.width = MATCH_PARENT并且在某个地方(我猜是在顶部)设置了尺寸。所以如果你遵循那棵树,很明显你的习惯View在树的底部,将never能够影响的大小Dialog.

那么现有的解决方案做了什么?

  • 两者都编码方法我的问题中提到的只是获得顶部View并修改其LayoutParams。显然,与所有View树中与父级匹配的对象,如果顶层设置为静态大小,则整个Dialog会改变尺寸。但如果顶层设置为WRAP_CONTENT,所有其余的View树中的物体仍然是抬头看树“匹配他们的父母”,而不是低头看树“包装他们的内容”。

如何解决问题:

说白了就是改变LayoutParams.width of all View影响路径中的对象WRAP_CONTENT.

我发现这只能在之后完成onStart的生命周期步骤DialogFragment叫做。所以onStart的实现如下:

@Override
public void onStart() { 
    // This MUST be called first! Otherwise the view tweaking will not be present in the displayed Dialog (most likely overriden)
    super.onStart();

    forceWrapContent(myCustomView);
}

然后函数适当修改View等级制度LayoutParams:

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 = LayoutParams.WRAP_CONTENT;
        }
    } while (current.getParent() != null);

    // Request a layout to be re-done
    current.requestLayout();
}

And here is the working result: enter image description here

这让我很困惑为什么整个Dialog不想成为WRAP_CONTENT具有明确的minWidth设置为处理适合默认大小的所有情况,但我确信这样做是有充分理由的(我有兴趣听到它)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有自定义视图的 AlertDialog:调整大小以包裹视图的内容 的相关文章

随机推荐