1
我可以通過以編程方式在GridLayout
中設置ImageView
的背景顏色來使UI看起來像這樣。 在ImageViews中加載畢加索的圖像在GridLayout中溢出?
我正在使用畢加索從這些網格中的服務器加載圖像。圖像正在延伸超出其定義的colspan和rowspan。我爲什麼得到這個?
網格佈局XML
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tableGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp"
app:alignmentMode="alignBounds"
app:columnOrderPreserved="true"
app:rowOrderPreserved="true"
app:useDefaultMargins="false"/>`
而且在Activity
private void setGrids(int totalRows, int totalCols) {
int total = gridList.size();
gridLayout.setColumnCount(totalCols);
gridLayout.setRowCount(totalRows);
for (int i = 0; i < total; i++) {
float rowWeight = Float.parseFloat(gridList.get(i).getRowSpan() + "F");
float colWeight = Float.parseFloat(gridList.get(i).getColSpan() + "F");
GridLayout.Spec rowSpan = GridLayout.spec(gridList.get(i).getStartRow(), gridList.get(i).getRowSpan(), rowWeight);
GridLayout.Spec colSpan = GridLayout.spec(gridList.get(i).getStartColumn(), gridList.get(i).getColSpan(), colWeight);
GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
rowSpan, colSpan);
GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.height = 0;
param.width = 0;
ImageView oImageView = new ImageView(this);
oImageView.setLayoutParams(new GridLayout.LayoutParams(param));
/*
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
oImageView.setBackgroundColor(color);
*/
Picasso.with(this).load(gridList.get(i).getProductImage()).noFade().into(oImageView);
oImageView.setAdjustViewBounds(true);
oImageView.setScaleType(ImageView.ScaleType.FIT_XY);
gridLayout.addView(oImageView, gridParam);
}
}
我的網格設置方法指導我,讓我可以在這些網格適合圖像正常。