2012-07-12 66 views
0

這裏是我的代碼:的Android改變的ImageView的背景中的自定義對話框不工作

private void setWeatherDialogInfoAndIcons(){ 
    WeatherGoogle weather = WeatherGoogle.getInstance("istanbul"); 
    String iconsPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/arla/images/hava/"; 
    String[] allData = weather.getCityData().split("<>"); 

    currentImage = (ImageView) customDialog.findViewById(R.id.imageViewcurrentWeatherImage); 
    currentInfo = (TextView) customDialog.findViewById(R.id.textViewCurrentInfo) ; 
    currentInfo.setText(allData[0] +"\n"+allData[1]+"\n"+allData[2]+"\n"+allData[3]); 

    currentImage.setBackgroundResource(R.drawable.air); 
    currentImage.setBackgroundDrawable(UiHelper.getDrawableFromSdcard(iconsPath + weather.getIconName(0))); 

    int j = 4; 
    for (int i = 0; i < dayInfos.size() ; i++) { 
     dayInfos.get(i).setText(allData[j] +"\nMin : "+ allData[j+1] +"\nMaks: "+ allData[j+2] 
       + "\n" + allData[j+3] ); 
     j+=4; 
    } 

} 

我可以改變textviews的文本,但是當我想設置imageviev的背景(currentImage)它不工作,它顯示對話框,但沒有背景Imageview

currentImage.setBackgroundDrawable(UiHelper.getDrawableFromSdcard(iconsPath + weather.getIconName(0))); 

我相信,我的方法getDrawableFromSdcard的作品,因爲我曾經在別的地方在我的代碼最後,我選中的圖標爲png和路徑。那麼問題是什麼?

回答

0
currentImage.setBackgroundDrawable(getResources.getDrawable(R.drawable.air)); 

從SD卡:

Bitmap picture = BitmapFactory.decodeFile("file_path"); 
imageView.setImageBitmap(picture); 

試試吧。祝你好運!

+0

它不能幫助我,因爲我試圖從SD卡獲取繪製! – ctugyildiz 2012-07-12 12:06:25

+0

我更新了我的答案。 – 2012-07-12 12:09:42

+0

謝謝,它的工作原理! – ctugyildiz 2012-07-12 12:13:56

-1

方法setBackgroundDrawable(Drawable background)已棄用。您必須改用setBackground(Drawable)。見reference

+0

我正在開發具有2.3.1版本Api設備的應用程序。Api級別9. – ctugyildiz 2012-07-12 12:04:19

+2

setBackground僅適用於API 16+,因此如果以前沒有檢查過,95%以上的設備會崩潰。 – 2012-08-09 14:20:52

相關問題