2

の背景として使用したいタイル化可能な画像がありListViewます。setCacheColorHint(android.R.color.transparent)を使用して、背景画像を正しく表示するように設定しましたsetBackgroundDrawable()

私の問題は、をスクロールするListViewと、画像が所定の位置に留まり、スクロールもしないことです。これは通常の動作だと思いますが、画像をテキストと一緒にスクロールしたいと思います。

自分でロールしListViewたり、すべてのセルのビューの背景を設定したりせずに、それを行う方法はありますか?これを動的にしたいので、私はこれにXMLを使用していません。

4

1 に答える 1

2

1 つのクラスを作成し、それを ListView :: で拡張します

public class MyCustomListView extends ListView
{
        private Bitmap background;

        public MyCustomListView(Context context, AttributeSet attrs) 
        {
            super(context, attrs);
            background = BitmapFactory.decodeResource(getResources(), R.drawable.yourImage);//yourImage means your listView Background which you want to move
        }

        @Override
        protected void dispatchDraw(Canvas canvas) 
        {
            int count = getChildCount();
            int top = count > 0 ? getChildAt(0).getTop() : 0;
            int backgroundWidth = background.getWidth();
            int backgroundHeight = background.getHeight();
            int width = getWidth();
            int height = getHeight();

            for (int y = top; y < height; y += backgroundHeight)
            {
                for (int x = 0; x < width; x += backgroundWidth)
                {
                    canvas.drawBitmap(background, x, y, null);
                }
            }
            super.dispatchDraw(canvas);
        }
}

XMLファイルで、次のようなリストビューを1つ取得します::
//要件に応じて他の属性を設定します

 <com.test.MyCustomListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
 </com.test.MyCustomListView>
于 2013-01-04T11:11:15.330 に答える