0

我试图用包含平铺背景的布局将我的 ImageView 的顶部和底部包围在画廊中。顶部似乎正在工作,但底部布局没有显示。任何想法为什么?这是我的getView():

public View getView(int position, View convertView, ViewGroup parent) {
            RelativeLayout container = new RelativeLayout(galleryContext);

            RelativeLayout topReel = new RelativeLayout(galleryContext);
            RelativeLayout bottomReel = new RelativeLayout(galleryContext);

            topReel.setBackgroundResource(R.drawable.film_top);
            bottomReel.setBackgroundResource(R.drawable.film_bottom);

            ImageView imageView = null;
            if (convertView != null) {  //we can reuse the view!
                imageView = (ImageView) convertView;
            } else {
                imageView = new ImageView(galleryContext);   //boo we have to make a new view
            }

            //Get a scaled Bitmap so that it doesn't use up all our memory

            Bitmap setBitmap = loadScaledBitmap(imageList[position].getAbsolutePath(), imageSizeInPixels);

            //set up our ImageView inside the gallery to display our Bitmap...

            imageView.setImageBitmap(setBitmap);
            imageView.setLayoutParams(new Gallery.LayoutParams(imageSizeInPixels, imageSizeInPixels));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setBackgroundResource(galleryBackground);

            RelativeLayout.LayoutParams topParams = new RelativeLayout.LayoutParams(imageSizeInPixels, 20);
            topParams.addRule(RelativeLayout.ABOVE, imageView.getId());
            topParams.addRule(RelativeLayout.CENTER_HORIZONTAL);


            RelativeLayout.LayoutParams bottomParams = new RelativeLayout.LayoutParams(imageSizeInPixels, 20);
            bottomParams.addRule(RelativeLayout.BELOW, imageView.getId());
            bottomParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

            RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(imageSizeInPixels, imageSizeInPixels);
            imageParams.addRule(RelativeLayout.BELOW, topReel.getId());
            imageParams.addRule(RelativeLayout.ABOVE, bottomReel.getId());
            imageParams.addRule(RelativeLayout.CENTER_VERTICAL);
            imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);


            container.addView(topReel, topParams);
            container.addView(bottomReel, bottomParams);
            container.addView(imageView, imageParams);

            return container;
        }
4

1 回答 1

0

这解决了问题,希望有人可以使用它:

public View getView(int position, View convertView, ViewGroup parent) {
            RelativeLayout container = new RelativeLayout(galleryContext);

            ImageView imageView = null;
            if (convertView != null) {  //we can reuse the view!
                imageView = (ImageView) convertView;
            } else {
                imageView = new ImageView(galleryContext);   //boo we have to make a new view
            }

            //Get a scaled Bitmap so that it doesn't use up all our memory

            Bitmap setBitmap = loadScaledBitmap(imageList[position].getAbsolutePath(), ((int)(imageSizeInPixels * .75)));

            //set up our ImageView inside the gallery to display our Bitmap...

            imageView.setImageBitmap(setBitmap);
            imageView.setLayoutParams(new Gallery.LayoutParams(imageSizeInPixels, imageSizeInPixels));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setBackgroundColor(Color.BLACK);

            RelativeLayout borderImg = new RelativeLayout(galleryContext);
            borderImg.setPadding(10, 5,10, 5);
            borderImg.setBackgroundColor(0xff000000);
            borderImg.addView(imageView);


            RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(imageSizeInPixels, imageSizeInPixels);
            imageParams.addRule(RelativeLayout.CENTER_VERTICAL);
            imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);


            container.setBackgroundResource(R.drawable.repeat_reel);
            container.setLayoutParams(new Gallery.LayoutParams(imageSizeInPixels, imageSizeInPixels+40));

            container.addView(borderImg, imageParams);

            return container;
        }
于 2012-07-19T14:44:33.477 回答