我正在以编程方式在RelativeLayout中动态添加ImageView,但我还需要使用onClick处理程序将删除图标/按钮与这些动态ImageView相关联。因此,如果我点击任何与该动态图像视图相关的删除图标,将被删除。
这是我的代码,我在布局中添加动态图像视图:
<security:authentication-manager id="authenticationManager">
<!-- <security:authentication-provider user-service-ref="userDao"> -->
<security:authentication-provider>
<security:user-service>
<security:user name="user" password="user"
authorities="ROLE_USER" />
<security:user name="xyz" password="xyz"
authorities="ROLE_USER" />
<security:user name="abcd" password="abcd"
authorities="ROLE_ADMIN" />
</security:user-service>
<!-- <security:password-encoder ref="passwordEncoder"></security:password-encoder> -->
</security:authentication-provider>
</security:authentication-manager>
答案 0 :(得分:1)
我建议您创建一个自定义组件。像这样:
R.layout.deletable_image
<merge>
是一个普通的xml-layout,<merge xmlns="...">
<ImageView android:id="@+id/image" .../>
<Button android:id="@+id/delete_button" .../>
</merge>
为根元素(因为DeletableImageView已经是LinearLayout。在merge-Tag中你有ImageView和Button。
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
final DeletableImageView div = new DeletableImageView(this);
div.setImage(...); // add this method to DeletableImageView
div.setListener(this);
// either implement these in your custom component or directly set it in the <ImageView> in R.layout.deletable_image
div.setScaleType(...);
div.setPadding(2, 2, 2, 2);
rl.addView(div);
在您的代码中,您不添加ImageView,而是添加自定义组件并将Listener设置为回调,因此您可以再次删除自定义组件。
deleteMe(DeletableImageView me)
最后一步是实施rl.removeView(me);
。您必须持有对RelativeLayout的引用,并且只需调用{{1}}