以编程方式添加Imageview(Getting Imageview added programmatically)

编程入门 行业动态 更新时间:2024-10-03 12:36:56
以编程方式添加Imageview(Getting Imageview added programmatically)

我正试图沿着屏幕移动一个以编程方式添加的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.

更多推荐

本文发布于:2023-07-09 10:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085580.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   Imageview   programmatically   added

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!