Android square imageview

时间:2011-07-11 00:04:50

标签: java android layout

我试图制作一张包含方形图像视图的表格(并且所有图片都具有相同的尺寸)。问题是图像具有不同的高度和宽度。 该表是3x3,我想: 每个单元格的宽度= 1/3(可能是布局重量?)和高度=宽度 如果在xml中有一种定义方法比在代码中定义更好。 请解释一下,因为我是一个布局权重=的菜鸟( 提前谢谢,

3 个答案:

答案 0 :(得分:4)

尝试scaleType属性。我想你想要centerCrop

答案 1 :(得分:1)

你也可以使用scaleType = fitXY将每个图像压缩到imageview的尺寸

答案 2 :(得分:1)

扩展imageveiw可能是最干净的解决方案。这对我有用:

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

    }

}

}

在xml中:

<com.your_package_name.Icon
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:src="@drawable/screen" />