verticalSpacing 设置个元素间的垂直距离
小案例:
主界面:
package com.example.gridview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.GridView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private GridView gvGoods; private SimpleAdapter adapter; private void setupView(){ gvGoods = (GridView)findViewById(R.id.gvGoods); String[] from = {"title","thumbId"}; int[] to = {R.id.tvTitle,R.id.ivThumbnail}; adapter = new SimpleAdapter(this,getData(),R.layout.item,from,to); gvGoods.setAdapter(adapter); } private List<HashMap<String,Object>> getData(){ ArrayList<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>(); int[] thumbs = {R.drawable.a,R.drawable.b,R.drawable.c, R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h,R.drawable.i, R.drawable.j}; String[] title = {"one","two","three","four","five" ,"six","seven","eight","nine"}; for(int i =0 ;i<9;i++){ HashMap<String, Object> item = new HashMap<String,Object>(); item.put("title", title[i]); item.put("thumbId", thumbs[i]); data.add(item); } return data; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setupView(); } }
布局:
<GridView android:layout_width="match_parent" android:layout_height="match_parent" android:numColumns="auto_fit" android:columnWidth="120dp" android:stretchMode="spacingWidth" android:horizontalSpacing="10dp" android:verticalSpacing="10dp" android:padding="10dp" android:id="@+id/gvGoods" xmlns:android="http://schemas.android.com/apk/res/android" />
音乐item.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" > <ImageView android:id="@+id/ivThumbnail" android:layout_width="120dp" android:layout_height="120dp" android:scaleType="fitCenter"/> <TextView android:id="@+id/tvTitle" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="15sp"/> </LinearLayout>