我正在我的android应用中创建ImageView
的列表,并为每个列表设置了负边距。
我正在使用它来将dp转换为像素:
public int dpToPixel(float dp) {
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
return (int)((dp * displayMetrics.density) + 0.5);
}
列表是这样的:
List<GridLayout> columnNLinesGrid = new ArrayList<GridLayout>();
List<ImageView> columnLines = new ArrayList<ImageView>();
for (int i = 1; i <= rowLayoutsCount; i++) {
columnNLinesGrid.add(new GridLayout(this));
GridLayout.LayoutParams colGridParam = new GridLayout.LayoutParams();
colGridParam.setMargins(0,0,0,0);
colGridParam.topMargin = dpToPixel(-10);
columnNLinesGrid.get(i - 1).setLayoutParams(colGridParam);
columnNLinesGrid.get(i - 1).setColumnCount(columnLayoutsCount);
linearLayouts.get(i - 1).addView(columnNLinesGrid.get(i - 1));
for (int j = 1; j <= columnLayoutsCount; j++) {
columnLines.add(new ImageView(this));
GridLayout.LayoutParams lineParam = new GridLayout.LayoutParams();
lineParam.rowSpec = GridLayout.spec(0);
lineParam.columnSpec = GridLayout.spec(j - 1);
if(j-1>0){
lineParam.leftMargin = (int) dpToPixel(23);
} else{
lineParam.leftMargin = 0;
}
lineParam.width = GridLayout.LayoutParams.WRAP_CONTENT;
lineParam.height = (int) dpToPixel(35);
columnLines.get(colLinesCount).setLayoutParams(lineParam);
columnLines.get(colLinesCount).setImageResource(R.drawable.linevert);
columnNLinesGrid.get(i - 1).addView(columnLines.get(colLinesCount));
colLinesCount++;
}
}
似乎colGridParam.topMargin = dpToPixel(-10)
无效。在其他一些情况下,我尝试使用负边距,但没有一个起作用。