I have a row View
that I am using in a ListView
.
This RowView
consists of an ImageView
on the left and a TextView
on the right in a horizontal LinearLayout
where the image is given 40% of the space and the text the remaining 60%.
I want the layout to handle the resizing of the image in the ImageView
in the following manner:
- Stretch the image horizontally so that it fills up given 40% of the
LinearLayout
- Resize the
ImageView
vertically to maintain the original aspect ratio
This is my approach to the layouts:
protected class RowView extends LinearLayout {
public ImageView iv = null;
public TextView tv = null;
private LinearLayout.LayoutParams ivlp = null;
private LinearLayout.LayoutParams tvlp = null;
public RowView(Context ct) {
super(ct);
setOrientation(LinearLayout.HORIZONTAL);
// Create the views
iv = new ImageView(ct);
ivlp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 0.4f);
iv.setLayoutParams(ivlp);
iv.setAdjustViewBounds(true);
iv.setScaleType(ScaleType.FIT_XY);
tv = new TextView(ct);
tvlp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0.4f);
tv.setLayoutParams(tvlp);
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
// Add the views
addView(iv);
addView(tv);
}
}
The result I am getting is something like this:
From what I understand:
- The
MATCH_PARENT
is correctly forcing the image to horizontally stretch to fill the 40% space - The
ImageView.setAdjustViewBounds(true)
is supposed to allow any resizing of theImageView
that is necessary which is what I want. The problem is none of theImageView.ScaleType
's seem to do what I want ImageView.ScaleType(ScaleType.FIT_XY)
is not what I want because it doesn't maintain the aspect ratio. But all the others that do, don't seem to stretch the height of theImageView
(which is makingImageView.setAdjustViewBounds(true)
pretty much useless).
What am I doing wrong? It seems wrong that something this simple could be so difficult?
Or do I actually have to go to the effort of extending an ImageView
and Overriding onMeasure
to force the resizing and aspect ratio?
Or is there a clever trick with Matrix
to get the job done?