3 回答

TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超7個(gè)贊
拉伸GridView列時(shí),有一個(gè)更簡(jiǎn)單的解決方案。只需使用以下方法覆蓋GridView項(xiàng)布局的onMeasure即可:
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊
或者,如果您想擴(kuò)展View,只需執(zhí)行一些非常簡(jiǎn)單的操作即可:
public class SquareImageView extends ImageView
{
public SquareImageView(final Context context)
{
super(context);
}
public SquareImageView(final Context context, final AttributeSet attrs)
{
super(context, attrs);
}
public SquareImageView(final Context context, final AttributeSet attrs, final int defStyle)
{
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec)
{
final int width = getDefaultSize(getSuggestedMinimumWidth(),widthMeasureSpec);
setMeasuredDimension(width, width);
}
@Override
protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh)
{
super.onSizeChanged(w, w, oldw, oldh);
}
}
值得注意的super.onMeasure()是不需要。onMeasured要求是您必須致電setMeasuredDimension。
- 3 回答
- 0 關(guān)注
- 663 瀏覽