200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > android添加imageview android – 以编程方式将ImageView添加到Layout

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

时间:2020-02-28 03:24:30

相关推荐

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/0901/1781440.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。