3 回答
TA贡献1818条经验 获得超3个赞
你有2个解决方案:
编写自己的自定义布局。这将是更难的解决方案(但可能被认为是正确的解决方案)。
GridView在代码中设置您的实际高度。例如:
RelativeLayout.LayoutParams lp =(RelativeLayout.LayoutParams)myGridView.getLayoutParams();
// lp.setMargins(0,0,10,10); //如果你有布局边距,你也必须设置它们
lp.height = measureRealHeight(...);
myGridView.setLayoutParams(LP);
该measureRealHeight()方法应该看起来像这样(希望我做对了):
private int measureRealHeight(...)
{
final int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
final double screenDensity = getResources().getDisplayMetrics().density;
final int paddingLeft = (int) (X * screenDensity + 0.5f); // where X is your desired padding
final int paddingRight = ...;
final int horizontalSpacing = (int) (X * screenDensity + 0.5f); // the spacing between your columns
final int verticalSpacing = ...; // the spacing between your rows
final int columnWidth = (int) (X * screenDensity + 0.5f);
final int columnsCount = (screenWidth - paddingLeft - paddingRight + horizontalSpacing - myGridView.getVerticalScrollbarWidth()) / (columnWidth + horizontalSpacing);
final int rowsCount = picsCount / columnsCount + (picsCount % columnsCount == 0 ? 0 : 1);
return columnWidth * rowsCount + verticalSpacing * (rowsCount - 1);
}
上面的代码应该适用于Android 1.5+。
- 3 回答
- 0 关注
- 581 浏览
添加回答
举报