2011-07-11 109 views
7

即時通訊試圖製作與方形圖像(和所有具有相同的大小)的表。問題是圖像有不同的高度和寬度。 桌子是3x3,我想: 每個單元格有一個=屏幕寬度的1/3(可能與佈局的重量?)和高度=寬度 好得多,如果有一種方法來定義在比代碼中的xml。 請解釋一下,因爲IM使用佈局權重=一個小白( 由於提前,Android廣場imageview

回答

1

那麼你也可以使用scaleType = fitXY擠每個圖像。進入的ImageView尺寸

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" />