Android GridView 只有顯示 one row 一列的解決方案

問題: 當 Android GridView 的 layout_height = wrap_content 時候, GridView 只會顯示第一列.

解決: 建立一個繼承 GridView 的 Class, 然後覆寫 onMeasure event :
 程式碼
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightSpec = heightMeasureSpec;
    
    if (getLayoutParams().height == LayoutParams.WRAP_CONTENT)
        heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE, MeasureSpec.AT_MOST);
    
    super.onMeasure(widthMeasureSpec, heightSpec);
}

參考網址: http://www.jayway.com/2012/10/04/how-to-make-the-height-of-a-gridview-wrap-its-content/

參考看看囉 ^^

Related Posts Plugin for WordPress, Blogger...