GridView常用的XML属性:
android:horizontalSpacing="5dp" 两列之间的间距是5dp
android:verticalSpacing="5dp" 两行之间的间距是5dp
android:stretchMode="spacingWidth" 缩放与列宽大小同步
android:numColumns="auto_fit" 本来是一行显示几个,现在改为自动分配
android:gravity 设置此组件中的内容在组件中的位置。可选的值有:top、bottom、left、right、center_vertical、fill_vertical、 center_horizontal、fill_horizontal、center、fill、clip_vertical可以多选,用“|”分开。
下面以实现手机界面图标为列
效果图如下
创建项目文件 activity_main.xml 代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <GridView android:id="@+id/gd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:horizontalSpacing="10dp" android:numColumns="3" android:verticalSpacing="10dp" /> </LinearLayout>java代码如下
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.ltg.doudizhu; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import android.widget.SimpleAdapter; import android.widget.AdapterView.OnItemClickListener; import android.widget.Toast; public class MainActivity extends Activity implements OnItemClickListener { private GridView gridView; int[] img = { R.drawable.dizhu, R.drawable.flash, R.drawable.game, R.drawable.java, R.drawable.jisuanqi, R.drawable.lock, R.drawable.phone, R.drawable.qq, R.drawable.weather, R.drawable.wifi, R.drawable.yinyue }; String[] imgname = { "斗地主", "Flash", "游戏", "java", "计算器", "关机", "电话", "QQ", "天气", "无线", "音乐" }; List<Map<String, Object>> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gridView = (GridView) findViewById(R.id.gd); list = new ArrayList<Map<String, Object>>(); SimpleAdapter simpleAdapter = new SimpleAdapter(this, getData(), R.layout.item, new String[] { "pic", "text" }, new int[] { R.id.image, R.id.text }); gridView.setAdapter(simpleAdapter); gridView.setOnItemClickListener(this); } private List<? extends Map<String, Object>> getData() { // TODO Auto-generated method stub for (int i = 0; i < img.length; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("pic", img[i]); map.put("text", imgname[i]); list.add(map); } return list; } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(this, "打开" + imgname[arg2], Toast.LENGTH_SHORT).show();//点击图标会提示点的那个 } }自制的布局文件item.xml文件代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageView android:id="@+id/image" android:layout_width="60dp" android:layout_height="60dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:text="test" android:textSize="20sp" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.gridview1" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" <h3> android:theme="@android:style/Theme.Black.NoTitleBar" > //更改主题的地方</h3> <!-- // android:theme="@style/AppTheme" --> <activity android:name="com.example.gridview1.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.ltg.doudizhu" > </activity> </application> </manifest>想要点击图标就进入相应的界面的话 需要添加代码在
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { }中 这里只展示一个
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // Toast.make(this, "打开"+, Toast.LENGTH_SHORT).sh Toast.makeText(this, "打开" + imgname[arg2], Toast.LENGTH_SHORT).show(); switch (arg2) { case 0: startActivity(new Intent(this, doudizhu.class)); break; } }