在Android中,ScrollView和GridView一起使用的話,會出現GridView顯示不完全的問題。針對這一個問題,我們對GridView進行處理。
首先先寫一個MyGridView類:
public class MyGridView extends GridView { public MyGridView(Context context, AttributeSet attrs) { super(context, attrs); } public MyGridView(Context context) { super(context); } public MyGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec( Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }這個直接可以使用,集成我們的GridView,然後重寫onMeasure方法即可。然後在我們所需要用到GridView的地方,xml佈局如下使用:
<com.view.MyGridView android:id="@+id/grid_stage" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/text_stage_name" android:layout_marginLeft="12dp" android:layout_marginRight="12dp" android:layout_marginTop="25dp" android:gravity="center" android:horizontalSpacing="12dp" android:numColumns="2" android:verticalSpacing="10dp"> </com.view.MyGridView>這樣使用以後,就可以讓我們的ScrollView下的GridView顯示完全了。
同樣的情況,ScrollView和listview的共同使用也是相同的道理。
但是實現這些以後,我們會發現還會存在一個問題,就是進入到我們的ScrollView的界面後,默認不是ScrollView的頂部,而是我們的GridView的部分。
是因爲我們的GridView默認獲取焦點。只需要在我們的代碼中加入一行代碼即可:
gridview.setFocusable(false);設置我們的gridview的焦點爲false即可。
這樣就完美解決了gridview顯示不完全的問題啦。