我的问题:一个ImageButton在GridLayout的屏幕之外。如何解决显示问题?我做错了什么?
原始图片:
问题图片:
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams paramsLinLyt = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(paramsLinLyt);
GridLayout gridLayout = new GridLayout(this);
gridLayout.setOrientation(GridLayout.VERTICAL);
int rows = 4;
int cols = 4;
gridLayout.setRowCount(rows);
gridLayout.setColumnCount(cols);
ImageCropProcessor imageCropProcessor = new ImageCropProcessor();
Bitmap[][] bitmaps = imageCropProcessor.cropImage(getResources(), R.drawable.tiger, rows, cols);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
GridLayout.Spec buttonRowSpec = GridLayout.spec(i);
GridLayout.Spec buttonSpecColumn = GridLayout.spec(j);
ImageButton imagebutton = new ImageButton(this);
imagebutton.setId(j + 1 + (i * 4));
imagebutton.setImageBitmap(bitmaps[i][j]);
gridLayout.addView(imagebutton, new GridLayout.LayoutParams(buttonRowSpec, buttonSpecColumn));
}
}
linearLayout.addView(gridLayout);
setContentView(linearLayout);
}
答案 0 :(得分:1)
您的图片太大而且无法放在屏幕上。
GridLayout
的尺寸由其内容决定。您尝试显示的内容太大,因此其中一部分位于可见屏幕之外。
您可以尝试缩放图像,使其适合屏幕。这样做时,应保留宽高比,以免图像被改变。
结帐Bitmap.createScaledBitmap()
。