我正试图沿着屏幕移动一个以编程方式添加的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);任何线索我应该如何继续实现我打算做的事情?
再见,
I’m trying to move along the screen an ImageView that was added programmatically. To do so
I’m searching its id through getIdentifier. Should this work? Code bellow:
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);How should I proceed to achieve what I’m intending to do?
最满意答案
在第一行中,您将获得可绘制的ID, 而不是 ImageView的ID。 您需要获取ImageView本身的ID。 如果您以编程方式创建/添加了ImageView ,则还需要以编程方式设置ID,如果您想使用findViewById再次获取它。 或者,您可以保留对实际View的引用。
No. In your first line you're getting the ID of a drawable, not of the ImageView. You need to get the ID of the ImageView itself. If you've programmatically created/added the ImageView you'll also need to programmatically set the ID if you want to use findViewById to get it again. Alternatively you could keep a reference to the actual View.
更多推荐
发布评论