可以在不使用imageview或xml文件的情况下显示droid中的图像

时间:2013-11-14 00:59:46

标签: android xml eclipse image imageview

我想知道是否有人知道是否可以动态显示图像,通过代码直接从eclipse中的res文件夹中拉出一个机器人应用程序,而无需在xml文件中设置imageview。

我对droid和编程一般都是新手,到目前为止我所有的图像都是用xml中的imageview来做的。很想知道是否可以将xml从中删除,以及任何建议如何。

谢谢!

3 个答案:

答案 0 :(得分:2)

LinearLayout linear = new LinearLayout(getApplicationContext());

ImageView img = new ImageView(getApplicationContext());

img.setBackgroundResource(R.drawable.ic_launcher);

linear.addView(IMG);

的setContentView(线性);

答案 1 :(得分:1)

您可以使用Java对象等效来扩展Java代码中的视图。对于ImageView:

ImageView imageView = new ImageView(Context);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.image_drawable);
addChildView(imageView); // add it to the screen. if in a fragment, need getView().addChildView(imageView);

答案 2 :(得分:1)

1.在xml布局中,将id添加到默认布局为 android:id =“@ + id / layout”

2.现在在java文件中:

RelativeLayout layout =(RelativeLayout)findViewById(R.id。 layout ); // xml默认布局

LinearLayout linear1 = new LinearLayout(getApplicationContext()); //动态创建布局

linear1.setBackgroundResource(R.drawable.ic_launcher); //将图像添加到动态布局

layout.addView(linear1); //将动态创建的布局添加到默认的xml布局

我在这里使用 LinearLayout 来创建可以使用任何人的动态布局。