gpt4 book ai didi

java - LinearLayout 不显示以编程方式添加的图像

转载 作者:行者123 更新时间:2023-12-02 04:48:02 25 4
gpt4 key购买 nike

我正在尝试制作一条红心线来指示玩家当前拥有的生命数。这是我的代码:

LifeGraphics.java

public class LifeGraphics {
public static ArrayList<ImageView> getLiast(Context context){
ImageView life1 = new ImageView(context);
life1.setImageResource(R.mipmap.heart);
ImageView life2 = new ImageView(context);
life1.setImageResource(R.mipmap.heart);
ImageView life3 = new ImageView(context);
life1.setImageResource(R.mipmap.heart);
ImageView life4 = new ImageView(context);
life1.setImageResource(R.mipmap.heart);
ImageView life5 = new ImageView(context);
life1.setImageResource(R.mipmap.heart);
ImageView[] inRetrunList = new ImageView[] {life1, life2, life3, life4, life5};
ArrayList<ImageView> returnList = new ArrayList<>();
returnList.addAll(Arrays.asList(inRetrunList));
return returnList;
}
}

GameActivity.java(摘录):

ArrayList<ImageView> livesGraphicsList = LifeGraphics.getLiast(getApplicationContext());
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
for(int i = 0; i < lives; i++){
layout.addView(livesGraphicsList.get(i), (i + 1));
}

结果如下:

Only one heart is shown

当他们失去一条生命时,数字会适当下降,并且图像会在最后一条生命后消失。还记录了layout.getChildCount()给了我6个(我猜是textview和5个添加的imageView)。有人可以解释一下为什么屏幕上没有显示吗?

提前谢谢您。

最佳答案

您仅将图像设置到第一个 ImageView。更准确地说,您设置了 5 次。

    ImageView life1 = new ImageView(context);
life1.setImageResource(R.mipmap.heart);
ImageView life2 = new ImageView(context);
life1.setImageResource(R.mipmap.heart); //Should be life2
ImageView life3 = new ImageView(context);
life1.setImageResource(R.mipmap.heart);//Should be life3
ImageView life4 = new ImageView(context);
life1.setImageResource(R.mipmap.heart);//Should be life4
ImageView life5 = new ImageView(context);
life1.setImageResource(R.mipmap.heart);//Should be life5

关于java - LinearLayout 不显示以编程方式添加的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29501100/

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