简体   繁体   中英

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?

I tried something like this, but no results, TextView is added to the bottom of my layout:

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 is generally not recommended and you shouldn't use it. I recommend to you use RelativeLayout and then you only need to add rule.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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