2014-01-31 86 views
0

我使用按鈕動態加載gridview。因此,我使用的是scrollview,但是如果我將wrap_content指定爲gridview的高度,則不會顯示所有按鈕。我不想爲gridview指定任何靜態高度。 這是我使用的代碼:如何分配wrap_content作爲高度動態加載gridview

<ScrollView 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 

       <LinearLayout 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="vertical" 
        android:paddingLeft="5dip" 
        android:paddingRight="5dip" > 

        <GridView 
         android:id="@+id/gridviewtable" 
         android:layout_width="fill_parent" 
         android:layout_height=wrap_content" 
         android:horizontalSpacing="10dp" 
         android:numColumns="4" 
         android:verticalSpacing="10dp" > 
        </GridView> 

       </LinearLayout> 
      </ScrollView> 

回答

0

android:layout_height="wrap_content"不能用於AdapterView(例如ListView和GridView的)的子類。 GridView必須得到每一行的高度才能計算出自己的高度。

刪除ScrollViewLinearLayout,你不需要它們。 GridView已經有了自己的內置滾動邏輯。

+0

如何爲gridview動態分配layout_params? –

+0

你爲什麼需要這個? –

+0

動態分配高度給gridview –

0

這個或類似的東西應該這樣做。 (代碼我記得) myImageView.setLayoutParams(new ImageView.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, (LinearLayout.LayoutParams.WRAP_CONTENT));

1

試試這個

int totalHeight = (count/<number of items in a row>); 

    if (count % <number of items in a row> != 0) { 
     totalHeight++; 
    } 

    ViewGroup.LayoutParams params = gridview.getLayoutParams(); 

    params.height = <height of a row> * totalHeight; 

    gridview.setLayoutParams(params); 
    gridview.requestLayout(); 
+0

這個答案的一個變種是唯一對我有用的東西! –

+0

謝謝盧克的評論。 – kukku

0

這是我之前很多答案的怪人,但它是唯一爲我工作至今:

private static void resizeGridView(GridView gridView, int rows) { 
    int measuredHeight = gridView.getMeasuredHeight(); 
    ViewGroup.LayoutParams params = gridView.getLayoutParams(); 
    params.height = measuredHeight * rows; 
    gridView.setLayoutParams(params); 
    gridView.requestLayout(); 
} 

在更新要顯示給gridView的內容後調用它。

0

您需要創建例如一個新的類

public class WrappingGridView extends GridView { 

public WrappingGridView(Context context) { 
    super(context); 
} 

public WrappingGridView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

public WrappingGridView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int heightSpec = heightMeasureSpec; 
    if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) { 
     // The great Android "hackatlon", the love, the magic. 
     // The two leftmost bits in the height measure spec have 
     // a special meaning, hence we can't use them to describe height. 
     heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
    } 
    super.onMeasure(widthMeasureSpec, heightSpec); 
} 

} 

你也需要在你的XML改變

<com.your.project.WrappingGridView 
       android:id="@+id/gridviewtable" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:horizontalSpacing="10dp" 
       android:numColumns="4" 
       android:verticalSpacing="10dp"/> 

終於在你的java類,你需要機會的對象GridView控件WrappingGridView