编辑 - 解决方案:
我最终找到了解决这个问题的方法。因为手动更改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:
<?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的AlertDialog占用整个对话框,保留它的尺寸,再加上两个按钮。通过标准方法实现它,如下所示:
我试图消除它上面和下面的填充。以下是其设置方式的代码:
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/goProDialogImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/whygoprodialogimage"/>
</LinearLayout>
代码:
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 :(得分:14)
我最终找到了解决这个问题的方法。因为手动更改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:
<?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>
答案 1 :(得分:7)
可能会迟到,但我认为在android:adjustViewBounds = "true"
中设置ImageView
会有所帮助。另外,我通常设置android:scaleType = "centerInside"
或android:scaleType = "centerCrop"
答案 2 :(得分:1)
AlertDialog
的CustomPanel有一个5dp的顶部和底部填充。您可以使用以下方法覆盖这些:
替换此行
dialog.setView(dialogLayout);
进入
dialog.setView(dialogLayout, 0, 0, 0, 0);
有关更多信息,请参阅这些链接,
答案 3 :(得分:1)
请设置您的视图的LinearLayout属性,即android:layout_height="match_parent"
。希望它能奏效。
答案 4 :(得分:1)
您的布局和图片视图都有layout_width="match_parent"
。这告诉视图(布局或图像视图)拉伸自身,以便填充父容器。尝试将此属性更改为wrap_content
。您可以在官方文档中找到有关该属性的详细说明 - here
将其设置为wrap_content
应该与高度具有相同的效果 - 它应该足够宽以绘制内容(在这种情况下为图像)而不是固定其大小(对于父级/容器)
答案 5 :(得分:1)
您可以尝试以下几种方法。您尝试使用此变体来查看它是否修复了视图比率。 Description
imageView.setScaleType(ScaleType.FIT_CENTER);
或者可能手动手动设置ImageView大小会有所帮助。
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(bitmapWidth, bitmapHeight);
image.setLayoutParams(layoutParams);
如果没有,还有其他一些方法,这些只是第一个想到的方法。
答案 6 :(得分:1)
我不太确定,但我认为它显示的对话框类型增加了填充。此类对话框不会隐藏整个活动。我用来隐藏整个活动的是:
/**
* 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);
}
您可能会找到满足您需求的选项。但是,如果要使用上述方法,则需要创建包含按钮和图像的布局。
检查选项:http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html
答案 7 :(得分:1)
我将此属性添加到我的ImageView。
<ImageView
(...)
android:adjustViewBounds="true" />
有效!