gpt4 book ai didi

java - 为什么 GridLayout 中的 ImageButton 会超出屏幕?

转载 作者:行者123 更新时间:2023-12-02 11:42:21 26 4
gpt4 key购买 nike

我的问题:ImageButton 在 GridLayout 中超出屏幕。如何解决显示问题?我做错了什么?

原图:

original picture

问题图片:

problem picture

这是我的代码:

@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);
}

最佳答案

您的图片太大,不适合您的屏幕。

GridLayout 的尺寸由其内容决定。您尝试显示的内容太大,因此其中一部分位于可见屏幕之外。

您可以尝试缩放图像,使其适合屏幕。这样做时,您应该保留宽度/高度比,以便图像不会改变。

查看Bitmap.createScaledBitmap()

关于java - 为什么 GridLayout 中的 ImageButton 会超出屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48461455/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com