Show AlertDialog with ImageView without any padding

后端 未结 8 1431
灰色年华
灰色年华 2020-12-23 15:04

EDIT - SOLUTION:

I ended up figuring out a way to solve this issue. Because manually changing the height of the ImageView removes the extra padding,

8条回答
  •  不知归路
    2020-12-23 15:37

    I ended up figuring out a way to solve this issue. Because manually changing the height of the ImageView removes the extra padding, I ended up finding the dimensions of the original image, and apply them to the ImageView once the ImageView dimensions can be calculated. Here is the final result:

    enter image description here

    This is the final working code:

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

    And the XML:

    
    
    
        
    
    
    

提交回复
热议问题