8

これは私のカスタムセレクターです(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に変わり、何を推測するのでしょうか。すべてが順調です、それはあるべきように繰り返されます。どこに問題があるのか​​、ハイライトが繰り返されるのに背景が繰り返されないのはなぜかわかりません。何かご意見は?

4

1 に答える 1

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);
                }
            }
        }
    }
}
于 2012-06-22T09:08:25.757 に答える