Java 动态创建的图像按钮不可见
我正在尝试创建一个ImageButton数组,所有这些按钮都显示在屏幕范围内,随机选择3个图像中的一个。问题是大多数按钮都不显示在屏幕上/根本不显示。我在运行时检查了坐标,它们都在屏幕范围内,但我看不到图像。大多数时候我能看到一个,有时两个。总共应该有12个 宽度和高度是在onCreate()中计算的屏幕度量,其中还调用createBallobs()。images[]数组包含可绘图项的idJava 动态创建的图像按钮不可见,java,android,imagebutton,Java,Android,Imagebutton,我正在尝试创建一个ImageButton数组,所有这些按钮都显示在屏幕范围内,随机选择3个图像中的一个。问题是大多数按钮都不显示在屏幕上/根本不显示。我在运行时检查了坐标,它们都在屏幕范围内,但我看不到图像。大多数时候我能看到一个,有时两个。总共应该有12个 宽度和高度是在onCreate()中计算的屏幕度量,其中还调用createBallobs()。images[]数组包含可绘图项的id private void createBalloons() { LinearLayout layo
private void createBalloons() {
LinearLayout layout = (LinearLayout)findViewById(R.id.container);
for (int i = 0; i < GameActivity.MAX_BALLOONS; i++) {
balloons[i] = new ImageButton(this);
setupBalloon(balloons[i], i);
layout.addView(balloons[i]);
}
}
private void setupBalloon(ImageButton b, int i) {
int imageId = (int)(Math.random() * images.length);
b.setImageResource(images[imageId]);
b.setBackgroundColor(Color.TRANSPARENT);
b.setScaleX(0.4f);
b.setScaleY(0.4f);
b.setX((float) (Math.random() * (width - b.getWidth())));
b.setY((float) (Math.random() * (height - b.getHeight())));
b.setVisibility(View.VISIBLE);
}
private void createballobs(){
LinearLayout布局=(LinearLayout)findViewById(R.id.container);
对于(int i=0;i
将设置气球替换为此
private void setupBalloon(ImageButton b, int i) {
int imageId = (int)(Math.random() * images.length);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
b.setLayoutParams(params);
b.setImageResource(images[imageId]);
b.setBackgroundColor(Color.TRANSPARENT);
b.setScaleX(0.4f);
b.setScaleY(0.4f);
b.setX((float) (Math.random() * (width - b.getWidth())));
b.setY((float) (Math.random() * (height - b.getHeight())));
b.setVisibility(View.VISIBLE);
}
此外,正如您提到的12个按钮,您的布局很可能无法容纳所有按钮,请尝试将ScrollView设置为LinearLayout的父级。您必须设置布局参数并添加所需的图像??您需要设置按钮的宽度和高度。我想您将无法使用
LinearLayout
,尝试使用框架布局
。在ScrollView中使用垂直方向的线性布局。