从安卓19 (KITKAT)开始,我不再能够在AlertDialog中用代码动态地调整图标的大小。它已经在Android 8- 16中工作了。现在,它只显示原来宽度和高度的图标--而不是调整大小的图标。
请注意,这些图像是从我的服务器上动态下载的,相同的对话框将根据用户的选择显示不同的图像。
这是过去为我工作的东西。对于如何更新安卓19+,有什么建议吗?
注意:我尝试过Bitmap.createScaledBitmap解决方案(请参阅注释行)。它没有效果。
Uri downloadedIcon = Uri.parse( "file://" + filePath );
BitMap bm = Media.getBitmap( getContentResolver(), downloadedIcon );
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
if (width >= 2000) {
bm.setDensity(840);
//bm = Bitmap.createScaledBitmap(bm,920,920,true);
} else if (width >= 1440) {
bm.setDensity(640);
//bm = Bitmap.createScaledBitmap(bm,700,700,true);
} else if(width >= 1024)
// etc
}
BitmapDrawable icon = new BitmapDrawable(bm);
AlertDialog theAlert = new AlertDialog.Builder(MyActivity.this)
.setTitle("My Title")
.setMessage("My Description")
.setIcon(-1).setIcon(icon)
theAlert.show();
发布于 2015-09-11 18:12:23
好的,我通过创建我自己的对话框布局来解决这个问题。
问题是,在Android 19+中,默认(系统)对话框布局将强制缩小图标的src。因此,要使它更大--你需要忘记图标,停止使用它。相反,在您自己的自定义布局中定义一个新的ImageView。
custom_dialog.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/dialog_pic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:adjustViewBounds="true" />
</RelativeLayout>
<TextView
android:id="@+id/desc"
android:text="DESCRIPTION GOES HERE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="serif"
android:textSize="17sp" />
</LinearLayout>
在充气ImageView之后,我用动态BitMap覆盖它,如下所示:
Uri downloadedIcon = Uri.parse( "file://" + filePath );
BitMap bm = Media.getBitmap( getContentResolver(), downloadedIcon );
View dialogView = getLayoutInflater().inflate(R.layout.my_custom_dialog, null);
ImageView pic = (ImageView) dialogView.findViewById(R.id.dialog_pic);
pic.setImageBitmap( bm );
TextView desc = (TextView) dialogView.findViewById(R.id.desc);
desc.setText("My Description Text");
AlertDialog theAlert = new AlertDialog.Builder(ScriptMenuActivity.this)
.setView(dialogView)
.setTitle("My Title Text")
.setNegativeButton("Back", null)
.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// do stuff
}}).create();
theAlert.show();
通过ImageView的“adjustViewBounds”和“scaleType”属性,按比例调整对话框图片的大小以适应对话框窗口。
https://stackoverflow.com/questions/32473652
复制