我正在尝试制作一个带有正方形图像视图的表格(并且都具有相同的大小)。问题是图像具有不同的高度和宽度。表格是3x3的,我想:每个单元格都有一个with = 1/3的屏幕宽度(可能是布局权重?)如果有一种方法可以在xml中定义,而不是在代码中定义,那么height =width就会更好。请解释它,因为我是一个菜鸟,布局权重=(提前谢谢,
发布于 2011-07-11 09:13:40
尝试scaleType属性。你想要centerCrop,我想。
发布于 2011-07-11 09:59:26
您也可以使用scaleType=fitXY将每个图像压缩到imageview的尺寸中
发布于 2014-06-10 19:52:40
扩展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" />https://stackoverflow.com/questions/6644583
复制相似问题