Я пытаюсь показать простое предупреждение, работающее над моей деятельностью, когда нет доступа к Интернету, однако по какой-то причине я не знаю, когда я установил значок для предупреждения, коробка становится беспорядочной, слишком большой. Ниже мой код:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
return new AlertDialog.Builder(SplashScreen.this)
.setTitle("Aviso")
.setIcon(R.drawable.alert_dialog_icon)
.setMessage("Acesso à internet não disponível. Clique OK para sair.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
SplashScreen.this.finish();
}
})
.create();
}
return null;
}
результат этого кода отображается на изображении ниже.
Что я делаю неправильно? Я что-то пропустил? Этот код был просто скопирован из исходного кода API-демки, который отлично работает на моем устройстве.
Большое спасибо Т
Может быть, значок , который вы используете, имеет большой размер. Увеличьте изображение до растрового изображения до небольшого размера и используйте BitmapDrawable..!! вместо id
Лучший способ - изменить размер значка. Если вы хотите сделать это программно, ознакомьтесь с этим.
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
Это растровое изображение можно использовать следующим образом
builder.setIcon(BMD);
Нет функций width()
и Height()
, ниже скорректирован код:
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.cyj);
int width = bitmapOrg.**getWidth**();
int height = bitmapOrg.**getHeight()**;
int newWidth = 45;
int newHeight = 45;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(0);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);