2011-03-23 23 views
1

我想顯示一個簡單的警報,當沒有互聯網連接可用時,我的活動工作,但是,由於某種原因,我不知道,當我設置警報圖標時,凌亂,太大。以下是我的代碼:Android對話框(不是自定義)問題

@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; 
} 

此代碼的結果顯示在下圖中。

device screenshot

我在做什麼錯?我錯過了什麼?此代碼直接從API演示源代碼中複製,該代碼在我的設備中完美工作。

非常感謝 牛逼

回答

1

可能是你正在使用的圖標是大尺寸的.... 縮放圖像的高達小尺寸,並使用BitmapDrawable ..! !而不是編號

+0

多數民衆贊成奇怪,因爲該對話框代碼和圖像,其中來自API演示源代碼,這在爲我工作正常複製仿真器和我的設備。 – Thiago 2011-03-23 12:21:35

+0

然後你說所使用的代碼和圖像也來自API演示源..也就是說它運行良好..但你說:「當我設置警報圖標時,框變得雜亂」意味着圖像是prblm。 ..so使用較小的圖像,並檢查一次... – Udaykiran 2011-03-23 12:29:33

+0

我改變了從api演示的ldpi圖像,它開始工作。我會繼續嘗試,因爲圖像質量下降,因爲我的屏幕是ldpi。謝謝 – Thiago 2011-03-23 12:47:45

2

最好的方法是更改​​圖標大小。如果你想這樣做編程看看這個..

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

1

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