2012-03-30 18 views
0

如果選擇或不選擇,我需要更改標籤圖標。如果我使用可繪製文件夾中的圖像,我正在使用以下選擇器。android內部存儲器加載標籤圖標

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/search_selected" 
     android:state_selected="true" /> 
    <item android:drawable="@drawable/search_normal" /> 
</selector> 

但是,我需要使用數據/數據中的圖像...並使用下面的代碼從那裏加載圖像。如何處理選擇器以便加載適當的圖像(如果選擇或不選擇),如果我需要從內部存儲器加載它?謝謝

ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon); 

File imgFile = new File(pathImage); 

if(imgFile.exists()){ 

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 

    icon.setImageBitmap(myBitmap); } 

    //icon.setImageResource(R.drawable.tab_search; 
+0

讓您同時擁有選擇和un_selected圖像。對? – 2012-03-30 10:15:19

+0

對!這兩個圖像都在數據/數據創建的文件夾上... – Jaume 2012-03-30 10:17:18

回答

1

使用StateListDrawables

讓您的兩個圖像作爲BitmapDrawable和做這樣的事情:在你的外部存儲器

StateListDrawable selector = new StateListDrawable(); 
selector.addState(new int[]{ android.R.attr.state_enabled }, yourDefaultBitmapDrawable); 
selector.addState(new int[]{ android.R.attr.state_selected }, yourSelectedBitmapDrawable); 
icon.setImageDrawable(selector); 
+0

毫米聽起來就是我所需要的!然而,給我一個錯誤:selector.addState(new int [] {android.R.attr.state_enabled},myBitmap);建議將「myBitmap」的類型更改爲「可繪製」。 – Jaume 2012-03-30 10:41:37

+0

Drawable d = new BitmapDrawable(myBitmap);解決了它!我非常感謝你的幫助,謝謝 – Jaume 2012-03-30 10:48:00

+0

啊,很酷,我已經更新了答案。快樂編碼! – 2012-03-30 11:01:57