以编程方式添加Imageview

时间:2013-04-26 22:00:47

标签: android

我正试图沿着屏幕移动一个以编程方式添加的ImageView。这样做

我正在通过getIdentifier搜索其id。这有用吗?代码如下:

int id = this.getResources().getIdentifier("lemon1", "drawable", this.getPackageName());
ImageView image = (ImageView)findViewById(id);
RelativeLayout.LayoutParams pos =   new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
pos.setMargins(0, 100, 0, 0);
image.setLayoutParams(pos);

我应该如何继续实现我打算做的事情?

1 个答案:

答案 0 :(得分:0)

没有。在第一行中,您将获得ImageView的可绘制ID 。您需要获取ImageView本身的ID。如果您要以编程方式创建/添加ImageView,则还需要programmatically set the ID,如果您想使用findViewById再次获取它。或者,您可以保留对实际View

的引用
相关问题