添加的ImageView的布局编程 [英] Adding ImageView to the Layout programmatically
问题描述
我要创建一个从屏幕的上部往下走的图像。
I want to create images which will go down from the upper part of the screen.
有关走到今天我有这样的:
For up to today I have this:
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);
的问题是,我要设置的ImageView上的布局xml文件,这code创建才刚刚1张图片。
The problems are that I have to set the imageview on the xml file on the layout and this code creates just 1 picture only.
欲该应用创建(在循环中例如)在屏幕的上部的一对图像进行编程,并让它们下拉屏幕。 (这里我用TranslateAnimation这里)。
我发现是这样的:
I want to program the app to create a couple of images (for example in a loop) in the upper part of the screen and have them drop down the screen. (here I use TranslateAnimation here). I found something like this:
ImageView mario = (ImageView) findViewById(R.drawable.mario);
但我不知道如何设置这是不是在XML文件中的ImageView的位置(这可能吗?)。
我虽然有关创建的LinearLayout,并把它添加到ImageView的。但如何在的LinearLayout添加到现有的布局?
But I don't know how to set the position of the ImageView which isn't in the xml file (is it possible?). I though about creating LinearLayout and add it to the ImageView. But how to add the linearlayout to the existing layout?
在此先感谢:)
推荐答案
您可以创建一个东西像布局
You can create a layout with something like
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);
您在布局传递什么参数()
显然将取决于你想要什么。然后,您可以创建单独的查看
添加到布局
您刚刚创建。但我强烈建议通过文档阅读,了解什么都可以在这里完成。
What parameters you pass in layout()
are obviously going to depend on what you want. Then you can create separate Views
to add to the Layout
you just created. But I highly advise reading through the docs to understand what all can be done here.
修改
ImageView myImage = new ImageView(this);
picLL.addView(myImage);
//set attributes for myImage;
这篇关于添加的ImageView的布局编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!