РЕДАКТИРОВАТЬ - РЕШЕНИЕ:
Я решил выяснить способ решения этой проблемы. Поскольку вручную изменение высоты 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>
ОРИГИНАЛЬНЫЙ ВОПРОС:
Моя цель - иметь AlertDialog, который имеет ImageView, который обрабатывает весь диалог, сохраняя его размеры и две кнопки. Внедряя его стандартными методами, он выглядит следующим образом:
Я пытаюсь устранить это дополнение над и под ним. Вот код для его настройки:
Разметка:
<?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();
Посмотрев этот ответ StackOverflow, я попытался реализовать это решение, потому что их проблема показалась схожей, но, хотя она ближе к тому, что я хочу, результат выглядит это:
Таким образом, он удалил прокладку, но изображение выглядело смятым. Вот код для реализации этого потенциального исправления:
// 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);
Что это заставляет изображение теперь выглядеть хлюпавшим? Вы можете сказать, что он избавился от дополнительной прокладки, но размеры изображения изменены.