android添加imageview,android – 以编程方式将ImageView添加到Layout

我想创建从屏幕上方向下的图像.

到今天为止我有这个:

ImageView mario = (ImageView) findViewById(R.id.mario);

TranslateAnimation anim = new TranslateAnimation(0f, 0f, 0, 400);

anim.setInterpolator(new LinearInterpolator());

anim.setRepeatCount(Animation.INFINITE);

anim.setDuration(800);

mario.startAnimation(anim);

问题是我必须在布局上的xml文件上设置imageview,这段代码只创建1张图片.

我想对应用程序进行编程,以在屏幕的上半部分创建几个图像(例如在循环中)并让它们下拉到屏幕上. (这里我在这里使用TranslateAnimation).

我找到了这样的东西:

ImageView mario = (ImageView) findViewById(R.drawable.mario);

但我不知道如何设置不在xml文件中的ImageView的位置(是否可能?).

我想创建LinearLayout并将其添加到ImageView.但是如何将linearlayout添加到现有布局?

提前致谢 :)

解决方法:

您可以使用类似的东西创建布局

View view = (View) findViewById(R.layout.current_layout); //the layout you set in `setContentView()`

LinearLayout picLL = new LinearLayout(CurrentActivity.this);

picLL.layout(0, 0, 100, 0);

picLL.setLayoutParams(new LayoutParams(1000, 60));

picLL.setOrientation(LinearLayout.HORIZONTAL);

((ViewGroup) view).addView(picLL);

你在layout()中传递的参数显然取决于你想要的.然后,您可以创建单独的视图以添加到刚刚创建的布局.但我强烈建议您阅读文档,了解这里可以做些什么.

编辑

ImageView myImage = new ImageView(this);

picLL.addView(myImage);

//set attributes for myImage;

标签:android,android-layout,android-imageview

来源: https://codeday.me/bug/20190901/1781440.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值