如何设置GridView 的整体宽度自动适应Iiem的个数
推荐于2017-11-27
展开全部
我之前也需要这样一个需求,在不同尺寸的手机中要显示GridView,也是需要能自动适应行和列的显示数量,但我没有找到那种让系统自动去适应的,所以我是根据里面Item的宽高来动态计算当前屏幕适合显示多少行和列。至于你说的设置间隙问题,第一张图中,你的水平horizontalSpacing设置为2,这是指单个Item中的内容距离这个Item边界的宽度,所以两个Item之间的宽度就会变成4,宽度自适应我目前是使用上面说的,通过Item的宽高来动态计算的,如果你有其它更好的方法,麻烦分享一下,这是我的生成GridView的部分代码:
final GridView gv = new GridView(this);
gv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
gv.setNumColumns(columns);
gv.setGravity(Gravity.CENTER);
gv.setPadding(resource_gridview_padding, resource_gridview_padding, resource_gridview_padding, 0);
gv.setHorizontalSpacing(resource_gridview_HorizontalSpacing);
gv.setVerticalSpacing(resource_gridview_VerticalSpacing);
ResourceGridViewItemAdapter expressionImageAdapter = new ResourceGridViewItemAdapter(this,l);
gv.setAdapter(expressionImageAdapter);
gv.setSelector(R.drawable.selector_resource_item);
gv.setOnItemClickListener(new MyOnItemClickListener(gv));
final GridView gv = new GridView(this);
gv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
gv.setNumColumns(columns);
gv.setGravity(Gravity.CENTER);
gv.setPadding(resource_gridview_padding, resource_gridview_padding, resource_gridview_padding, 0);
gv.setHorizontalSpacing(resource_gridview_HorizontalSpacing);
gv.setVerticalSpacing(resource_gridview_VerticalSpacing);
ResourceGridViewItemAdapter expressionImageAdapter = new ResourceGridViewItemAdapter(this,l);
gv.setAdapter(expressionImageAdapter);
gv.setSelector(R.drawable.selector_resource_item);
gv.setOnItemClickListener(new MyOnItemClickListener(gv));
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询