im trying to make a table with square imageviews (and all with the same size). The problem is that the images have different height and width. The table is 3x3 and i'd like to: each cell have a with = 1/3 of the screen width (maybe with the layout weight?) and height =width Much better if there is a way of define that in the xml than in code. Please explain it because im a noob with layouts weights =( Thanks in advance,
Asked
Active
Viewed 2,770 times
3 Answers
1
Extending imageveiw might be the cleanest solution. This works for me:
public class Icon extends ImageView {
public Icon(final Context context) {
super(context);
}
public Icon(final Context context, final AttributeSet attrs) {
super(context, attrs);
}
public Icon(final Context context, final AttributeSet attrs,
final int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int width, int height) {
super.onMeasure(width, height);
int measuredWidth = getMeasuredWidth();
int measuredHeight = getMeasuredHeight();
if (measuredWidth > measuredHeight) {
setMeasuredDimension(measuredHeight, measuredHeight);
} else {
setMeasuredDimension(measuredWidth, measuredWidth);
}
}
}
And in xml:
<com.your_package_name.Icon
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/screen" />

Illegal Argument
- 10,090
- 2
- 44
- 61
1
well you can also use scaleType=fitXY to squeeze each image into the imageview's dimensions

Kevin Qiu
- 1,616
- 1
- 13
- 15