简体   繁体   English

将textview放置在单击的imageview的顶部

[英]Position textview on top of clicked imageview

Is there a way to programatically add TextView and show it right next to the ImageView user clicked on? 有没有一种方法可以以编程方式添加TextView并将其显示在所单击的ImageView用户旁边?

I tried something like this, but no results, TextView is added to the bottom of my layout: 我尝试了类似的方法,但是没有结果, TextView被添加到布局的底部:

TextView score = new TextView(getBaseContext());
score.setText("Image clicked");
Rect rectf = new Rect();
im.getLocalVisibleRect(rectf);
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 300, 270);
score.setLayoutParams(params);
layout.addView(score);

First thing, never use AbsoluteLayout , never! 第一件事,永远不要使用AbsoluteLayout ,永远不要! AbsoluteLayout is generally not recommended and you shouldn't use it. 通常不建议使用AbsoluteLayout,因此不应使用它。 I recommend to you use RelativeLayout and then you only need to add rule. 我建议您使用RelativeLayout ,然后只需要添加规则。

Try to use following snippet of code: 尝试使用以下代码段:

relativeLayout = new RelativeLayout(this);
par = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
par.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
relativeLayout.addView(score, par);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM