android 对话框 不显示图片,android – 显示AlertDialog与ImageView没有任何填充

编辑 – 解决方案:

我最终想出了解决这个问题的方法.因为手动更改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);

}

});

而XML:

android:orientation="vertical"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:id="@+id/goProDialogImage"

android:layout_width="wrap_content"

android:layout_height="350dp"

android:src="@drawable/whygoprodialogimage"/>

原题:

我的目标是要有一个具有ImageView的AlertDialog,它可以占据整个对话框,保留它的维度,加上两个按钮.通过标准方法实现它,它看起来像下面这样:

我正在努力消除上方和下方的填充.以下是它的设置代码:

布局:

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/goProDialogImage"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:src="@drawable/whygoprodialogimage"/>

码:

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

dialog.show();

在看了this StackOverflow answer之后,我尝试实现这个解决方案,因为他们的问题似乎相似,但即使它更接近我现在想要的结果如下:

所以它消除了填充,但图像看起来很挤.以下是此潜在修复实现的代码:

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

这是什么导致图像现在看起来很挤压?您可以告诉它摆脱了额外的填充,但图像尺寸已更改.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值