2011-09-14 120 views
8

這是我的自定義選擇器(StateListDrawable)StateListDrawable和瓷磚位圖

<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:drawable="@drawable/common_cell_background" /> 
    <item 
     android:state_pressed="true" 
     android:drawable="@drawable/common_cell_background_highlight" /> 
    <item 
     android:state_focused="true" 
     android:drawable="@drawable/common_cell_background_highlight" /> 
    <item 
     android:state_selected="true" 
     android:drawable="@drawable/common_cell_background_highlight" /> 
</selector> 

兩者common_cell_background和common_cell_background_highlight是XML。下面的代碼:

common_cell_background.xml

<bitmap 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/common_cell_background_bitmap" 
    android:tileMode="repeat" 
    android:dither="true"> 
</bitmap> 

common_cell_background_highlight.xml

<bitmap 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/common_cell_background_bitmap_highlight" 
    android:tileMode="repeat" 
    android:dither="true"> 
</bitmap> 

位圖也完全一樣。高光只是輕一點,沒有其他區別。這兩個位圖都是PNG文件。

現在我設定

convertView.setBackgroundResource(R.drawable.list_item_background); 

,這裏是問題。我的common_cell_background不重複,它被拉長了。但是當我觸摸我的列表背景的單元格時,會有什麼驚奇變成了common_cell_background_highlight,並猜測是什麼?一切都很好,它應該重複。我不知道問題出在哪裏,爲什麼我的背景不會重複,而突出顯示。有什麼想法嗎?

+0

這裏同樣的問題。看起來像是隨機的:有時位圖會被正確地重複,有時它會延伸到 – iseeall

+0

這裏同樣的問題。使我抓狂。請發佈解決方案,如果你找到一個。謝謝你! – dineth

+0

請參閱:http://stackoverflow.com/a/7615120/1037294 –

回答

3

這是錯誤,它是固定在ICS,看到這樣的回答:https://stackoverflow.com/a/7615120/1037294

這裏是一個解決辦法:https://stackoverflow.com/a/9500334/1037294

注意,該解決方法僅適用於BitmapDrawable,對於其他類型的可繪製的像StateListDrawable,你需要做額外的工作。這裏是我用的:

public static void fixBackgrndTileMode(View view, TileMode tileModeX, TileMode tileModeY) { 
    if (view != null) { 
     Drawable bg = view.getBackground(); 

     if (bg instanceof BitmapDrawable) { 
      BitmapDrawable bmp = (BitmapDrawable) bg; 
      bmp.mutate(); // make sure that we aren't sharing state anymore 
      bmp.setTileModeXY(tileModeX, tileModeY); 
     } 
     else if (bg instanceof StateListDrawable) { 
      StateListDrawable stateDrwbl = (StateListDrawable) bg; 
      stateDrwbl.mutate(); // make sure that we aren't sharing state anymore 

      ConstantState constantState = stateDrwbl.getConstantState(); 
      if (constantState instanceof DrawableContainerState) { 
       DrawableContainerState drwblContainerState = (DrawableContainerState)constantState; 
       final Drawable[] drawables = drwblContainerState.getChildren(); 
       for (Drawable drwbl : drawables) { 
        if (drwbl instanceof BitmapDrawable) 
         ((BitmapDrawable)drwbl).setTileModeXY(tileModeX, tileModeY); 
       } 
      } 
     } 
    } 
} 
+0

謝謝。最後修復它。 –

+0

如何將它應用於xml中定義的Drawable? –