Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 显示带有ImageView的AlertDialog,无任何填充_Android_Dialog_Imageview_Android Alertdialog - Fatal编程技术网

Android 显示带有ImageView的AlertDialog,无任何填充

Android 显示带有ImageView的AlertDialog,无任何填充,android,dialog,imageview,android-alertdialog,Android,Dialog,Imageview,Android Alertdialog,编辑-解决方案: 我终于想出了解决这个问题的办法。由于手动更改ImageView的高度会删除额外的填充,因此我最终找到了原始图像的尺寸,并在计算ImageView尺寸后将其应用于ImageView。以下是最终结果: 这是最终工作代码: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setPositiveButton("Get Pro", new DialogInterface.OnCli

编辑-解决方案:


我终于想出了解决这个问题的办法。由于手动更改ImageView的高度会删除额外的填充,因此我最终找到了原始图像的尺寸,并在计算ImageView尺寸后将其应用于ImageView。以下是最终结果:

这是最终工作代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        final AlertDialog dialog = builder.create();
        LayoutInflater inflater = getLayoutInflater();
        View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
        dialog.setView(dialogLayout);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog.show();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface d) {
                ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
                Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                        R.drawable.whygoprodialogimage);
                float imageWidthInPX = (float)image.getWidth();

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Math.round(imageWidthInPX),
                        Math.round(imageWidthInPX * (float)icon.getHeight() / (float)icon.getWidth()));
                image.setLayoutParams(layoutParams);


            }
        });
AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        final AlertDialog dialog = builder.create();
        LayoutInflater inflater = getLayoutInflater();
        View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
        dialog.setView(dialogLayout);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog.show();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface d) {
                ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
                Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                        R.drawable.whygoprodialogimage);
                float imageWidthInPX = (float)image.getWidth();

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Math.round(imageWidthInPX),
                        Math.round(imageWidthInPX * (float)icon.getHeight() / (float)icon.getWidth()));
                image.setLayoutParams(layoutParams);


            }
        });
以及XML:

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

    <ImageView
        android:id="@+id/goProDialogImage"
        android:layout_width="wrap_content"
        android:layout_height="350dp"
        android:src="@drawable/whygoprodialogimage"/>

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

    <ImageView
        android:id="@+id/goProDialogImage"
        android:layout_width="wrap_content"
        android:layout_height="350dp"
        android:src="@drawable/whygoprodialogimage"/>

</LinearLayout>
在查看之后,我尝试实施该解决方案,因为他们的问题看起来很相似,但即使它更接近我现在想要的,结果如下所示:

因此,它消除了填充,但图像看起来被压扁了。以下是此潜在修复程序实现的代码:

// Get screen size
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int screenWidth = size.x;
        int screenHeight = size.y;

        // Get target image size
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.whygoprodialogimage);
        int bitmapHeight = bitmap.getHeight();
        int bitmapWidth = bitmap.getWidth();

        // Scale the image down to fit perfectly into the screen
        // The value (250 in this case) must be adjusted for phone/tables displays
        while(bitmapHeight > (screenHeight - 250) || bitmapWidth > (screenWidth - 250)) {
            bitmapHeight = bitmapHeight / 2;
            bitmapWidth = bitmapWidth / 2;
        }

        // Create resized bitmap image
        BitmapDrawable resizedDialogImage = new BitmapDrawable(context.getResources(), Bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, false));

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        AlertDialog dialog = builder.create();
        LayoutInflater inflater = getLayoutInflater();
        View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
        dialog.setView(dialogLayout);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Without this line there is a very small border around the image (1px)
        dialog.getWindow().setBackgroundDrawable(null);

        dialog.show();
        ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
        image.setBackground(resizedDialogImage);

是什么导致图像现在看起来被压扁了?你可以看出它去掉了额外的填充,但是图像尺寸发生了变化。

AlertDialog的CustomPanel有一个5dp的顶部和底部填充。您可以使用以下方法覆盖这些选项:

更换这条线

dialog.setView(dialogLayout);
进入

有关更多信息,请参阅以下链接:


请设置视图的LinearLayout属性,即android:layout\u height=“match\u parent”。希望它能工作。

您的布局和图像视图都有
布局\u width=“match\u parent”
。这会告诉视图(布局视图或图像视图)拉伸自身,以便填充父容器。尝试将此属性更改为
wrap\u content
。您可以在官方文件中找到有关该物业的详细说明-


将其设置为“包裹内容”(wrap\u content)应该与高度具有相同的效果-它应该足够宽以绘制内容(本例中的图像),而不是固定其大小(与父级/容器的大小相同)

以下是一些您可以尝试的方法。您可以尝试使用它的变体,以查看它是否修复了视图比率

或者手动设置ImageView的大小可能会有所帮助

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(bitmapWidth, bitmapHeight);
image.setLayoutParams(layoutParams);

如果没有,还有一些其他的方法,这些只是第一个想到的方法。

我不太确定,但我认为这是您显示的添加填充的对话框类型。这种类型的对话框不会隐藏整个活动。我用来隐藏整个活动的是:

/**
 * Show the overlay using the WindowManager class.
 * @param overlay The overlay that needs to be shown.
 */
public void showOverlay(View overlay) {
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
            PixelFormat.TRANSLUCENT);

    getWindowManager().addView(overlay, params);
}
你可能会找到一个满足你需要的选项。但是,如果要使用上述方法,则需要创建包含按钮和图像的布局


查看选项:

可能已经晚了,但我认为在
图像视图中设置
android:adjustViewBounds=“true”
可能会有所帮助。另外,我通常设置
android:scaleType=“centerInside”
android:scaleType=“centerCrop”
我最终找到了解决这个问题的方法。由于手动更改ImageView的高度会删除额外的填充,因此我最终找到了原始图像的尺寸,并在计算ImageView尺寸后将其应用于ImageView。以下是最终结果:

这是最终工作代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        final AlertDialog dialog = builder.create();
        LayoutInflater inflater = getLayoutInflater();
        View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
        dialog.setView(dialogLayout);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog.show();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface d) {
                ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
                Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                        R.drawable.whygoprodialogimage);
                float imageWidthInPX = (float)image.getWidth();

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Math.round(imageWidthInPX),
                        Math.round(imageWidthInPX * (float)icon.getHeight() / (float)icon.getWidth()));
                image.setLayoutParams(layoutParams);


            }
        });
AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setPositiveButton("Get Pro", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).setNegativeButton("No thanks", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        final AlertDialog dialog = builder.create();
        LayoutInflater inflater = getLayoutInflater();
        View dialogLayout = inflater.inflate(R.layout.go_pro_dialog_layout, null);
        dialog.setView(dialogLayout);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog.show();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface d) {
                ImageView image = (ImageView) dialog.findViewById(R.id.goProDialogImage);
                Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
                        R.drawable.whygoprodialogimage);
                float imageWidthInPX = (float)image.getWidth();

                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(Math.round(imageWidthInPX),
                        Math.round(imageWidthInPX * (float)icon.getHeight() / (float)icon.getWidth()));
                image.setLayoutParams(layoutParams);


            }
        });
以及XML:

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

    <ImageView
        android:id="@+id/goProDialogImage"
        android:layout_width="wrap_content"
        android:layout_height="350dp"
        android:src="@drawable/whygoprodialogimage"/>

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

    <ImageView
        android:id="@+id/goProDialogImage"
        android:layout_width="wrap_content"
        android:layout_height="350dp"
        android:src="@drawable/whygoprodialogimage"/>

</LinearLayout>

我将此属性添加到ImageView中

<ImageView
  (...)
  android:adjustViewBounds="true" />


成功了

5dp不是问题,它与其他东西有关,因为它比5dp大得多。遗憾的是,这没有改变任何东西。我将两者都更改为包装内容,它保持不变。这最终不是问题,解决方案现在位于最顶端。您可以通过计算宽度并为图像视图指定350dp的静态高度来正确显示特定图像。但它可能并不适用于所有类型的图像。在图像为横向或小于指定高度的情况下,此操作将失败。最好也为图像视图提供动态高度。此外,您希望对图像的最大宽度和高度有一个限制。您应该发布一个答案并接受它,而不是编辑您的问题,这样其他人就不会浪费时间了Plistener是在show方法之后添加的,它将如何工作。