stretchMode屬性值的作用是設置GridView中的條目以什么縮放模式去填充剩余空間。參數stretchMode 可選值為:none,spacingWidth,columnWidth, spacingWidthUniform

注意:spaceWidth和spacingWidthUniform是有差別的,下面通過一個例子說明一下,本人手機屏幕4.7英寸,分辨率為1280×720
1.建立一個Android項目
界面布局文件activity_main.xml如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="test" /> <GridView android:id="@+id/gridview1" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:numColumns="3" android:columnWidth="80dp" android:gravity="center" android:horizontalSpacing="8dp" android:verticalSpacing="8dp" android:stretchMode="none" /> </LinearLayout>
字符串文件strings.xml如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">gridviewtest</string> <string name="c1">F00</string> <string name="c2">0F0</string> <string name="c3">00F</string> <string name="c4">FF0</string> <string name="c5">F0F</string> <string name="c6">0FF</string> <string name="c7">07F</string> <string name="c8">F07</string> <string name="c9">70F</string> </resources>
顏色文件colors.xml如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="c1">#F00</color> <color name="c2">#0F0</color> <color name="c3">#00F</color> <color name="c4">#FF0</color> <color name="c5">#F0F</color> <color name="c6">#0FF</color> <color name="c7">#07F</color> <color name="c8">#F07</color> <color name="c9">#70F</color> </resources>
2.編寫代碼,如下:
public class MainActivity extends Activity { int []colors=new int[] { R.color.c1,R.color.c2,R.color.c3, R.color.c4,R.color.c5,R.color.c6, R.color.c7,R.color.c8,R.color.c9 }; int []texts=new int[] { R.string.c1,R.string.c2,R.string.c3, R.string.c4,R.string.c5,R.string.c6, R.string.c7,R.string.c8,R.string.c9 }; Button button1; GridView gridview1; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button)findViewById(R.id.button1); gridview1=(GridView)findViewById(R.id.gridview1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int width=gridview1.getColumnWidth(); int widthSpace=gridview1.getHorizontalSpacing(); Toast.makeText(MainActivity.this, "columnWidth:"+width+",widthSpace:"+widthSpace, Toast.LENGTH_LONG).show(); } }); final BaseAdapter baseAdapter=new BaseAdapter() { @Override public int getCount() { return texts.length; } @Override public Object getItem(int arg0) { return getResources().getString(texts[arg0]); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int position, View view, ViewGroup viewGroup) { TextView textView=new TextView(MainActivity.this); textView.setText(getItem(position).toString()); textView.setTextSize(20); textView.setGravity(Gravity.CENTER); textView.setBackgroundResource(colors[position]); textView.setWidth(60); textView.setHeight(60); return textView; } }; gridview1.setAdapter(baseAdapter); } }
3.測試
當將界面布局文件中GridView的stretchMode設為none,點擊按鈕,輸出的信息為columnWidth:160,widthSpace:16

當將界面布局文件中GridView的stretchMode設為spacingWidth,點擊按鈕,輸出的信息為columnWidth:160,widthSpace:120

當將界面布局文件中GridView的stretchMode設為columnWidth,點擊按鈕,輸出的信息為columnWidth:229,widthSpace:16

當將界面布局文件中GridView的stretchMode設為spacingWidthUniform,點擊按鈕,輸出的信息為columnWidth:160,widthSpace:68

