在GridView中显示SquareTextView中的所有项目

时间:2014-04-10 12:50:08

标签: java android android-gridview

我想在GridView中显示所有项目(SquareTextView)但在某些分辨率上并非所有项目都可见且用户需要滚动。我想在不滚动的情况下为用户显示所有内容。

SquareTextView包含方法(将此方法添加到CGridView没有帮助):

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

//项目与其父项匹配 如何计算网格宽度/高度以显示所有内容? 我看着: Gridview height gets cut 但答案没有帮助。

1 个答案:

答案 0 :(得分:0)

我修复了添加到自定义gridView的问题:

  @Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    final int width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
    final int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
    if (width > height) {
        setMeasuredDimension(height, height);
    } else {
        setMeasuredDimension(width, width);
    }


    Log.v(TAG, "sizes are h: " + height + " w: " + width);
}


@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    /**
     * Changed Size of TextView from "width" x "height" to "width" x "width"
     * to look like square*/
    super.onSizeChanged(w, w, oldw, oldh);

    /**
     * Hack -
     * if height is bigger then width
     * set height size to width
     * if width is bigger than height then
     * set width size to height
     */
    Log.v(TAG, "sizes2 are h: " + h + " w: " + w);
    if (getLayoutParams() != null && w < h) {
        getLayoutParams().height = w;
        setLayoutParams(getLayoutParams());
    }
    else if(getLayoutParams() != null && w > h){
        getLayoutParams().width = h;
        setLayoutParams(getLayoutParams());
    }
} 

这样我的gridView看起来像方形,所有项目都在设备上可见。如果有人遇到同样的问题应该会有所帮助