Change border style in Gallery

陌路散爱 提交于 2019-12-07 02:05:53

问题


I am taking this tutorial. As you can see on this image, the standard grey border is applied to all elements in the gallery. I would like to remove this rather ugly border, or, actually, make it a 1 px border instead (or just so the images can be distinguished from each other). I tried removing this line:

imgView.setBackgroundResource(GalItemBg);

That removes the border, but then the images overlap each other and it's still not very pretty.

So, how do I change the border? And how do I get the gallery elements to not overlap each other?


回答1:


I'm sure you've already discovered an answer for this problem, but as there's none posted, here's one now!

You can place each of your imgView objects inside of a RelativeLayout with a black background and a 1 padding. Then return the RelativeLayout object containing the ImageView instead of returning the ImageView itself.

public View getView(int position, View convertView, ViewGroup parent)
{
    ImageView imgView = new ImageView(m_Context);
    // do stuff initializing your imgView as before
    RelativeLayout borderImg = new RelativeLayout(m_Context);
    borderImg.setPadding(1,1,1,1);
    borderImg.setBackgroundColor(0xff000000);
    borderImg.addView(imgView);
    return borderImg;
}

Finally, it is much easier to enforce a spacing between your Gallery images by using the Gallery method setSpacing(int pixels) to put a space between each Gallery object. You won't get the black border around them, but the Gallery background will be visible between and behind them.



来源:https://stackoverflow.com/questions/4830173/change-border-style-in-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!