I have a pretty simple layout – an image on the right with text on the left. The text should wrap and not overlap the image. Both are centered vertically to the final height of the container.
RelativeLayout looked appropriate, aligning the image to the parent’s right edge, and the textview’s right edge with the image’s left edge. The docs make it sound like RelativeLayout.LEFT_OF should do this.
Here’s what I have. The text overlays the image. I need the textview to be only as large as the available space.
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lp.addRule(RelativeLayout.CENTER_VERTICAL);
ImageView logo = new ImageView(context);
logo.setImageResource(R.drawable.whatever);
logo.setId(0);
addView(logo, lp);
lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.LEFT_OF, 0);
lp.addRule(RelativeLayout.CENTER_VERTICAL);
TextView header = new TextView(context);
header.setText("Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.");
addView(header, lp);
I’d very much prefer to do this programmatically (as opposed to XML), for reasons too long and uninteresting to mention.
Any insight would be appreciated. TYIA.
Don’t set your logoId to 0.
Use
setId(1231231)instead or ID number something like that and do it likelp.addRule(RelativeLayout.LEFT_OF, logo.getId());you can also usesetMargins(...)if you need them.Cheers