其实它是跟listview相似的,如果你看下它跟listview的继承关系,就很容易理解了
public class GridView extends AbsListView { /** * Disables stretching. * * @see #setStretchMode(int) */ public static final int NO_STRETCH = 0; /** * Stretches the spacing between columns. * * @see #setStretchMode(int) */ public static final int STRETCH_SPACING = 1; /** * Stretches columns. * * @see #setStretchMode(int) */ public static final int STRETCH_COLUMN_WIDTH = 2; /** * Stretches the spacing between columns. The spacing is uniform. * * @see #setStretchMode(int) */ public static final int STRETCH_SPACING_UNIFORM = 3; }
而且,它里面并没有selector的方法,那我们继续看AbListView这个类
public abstract class AbsListView extends AdapterView<ListAdapter> implements TextWatcher, ViewTreeObserver.OnGlobalLayoutListener, Filter.FilterListener, ViewTreeObserver.OnTouchModeChangeListener, RemoteViewsAdapter.RemoteAdapterConnectionCallback { public void setSelector(int resID) { setSelector(getResources().getDrawable(resID)); } public void setSelector(Drawable sel) { if (mSelector != null) { mSelector.setCallback(null); unscheduleDrawable(mSelector); } mSelector = sel; Rect padding = new Rect(); sel.getPadding(padding); mSelectionLeftPadding = padding.left; mSelectionTopPadding = padding.top; mSelectionRightPadding = padding.right; mSelectionBottomPadding = padding.bottom; sel.setCallback(this); updateSelectorState(); } }
可以看到它里面是有这个方法的。
在xml中设定的方法如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_share_flipper" android:layout_width="fill_parent" android:layout_height="wrap_content" > <GridView android:id="@+id/gv_share_gridview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:hapticFeedbackEnabled="true" <!--跟listview的使用方法一样-->
android:listSelector="@drawable/gridview_selector" android:numColumns="4" android:scrollbars="none" android:stretchMode="columnWidth" > </GridView> </LinearLayout>