GrideView定义时实际上是定义每一个单元格的样式。
最右边的ListView定义时是每一行的样式
当我们想表达二维的行列分布时就可以选择GrideView
点击一个图标显示Toast
下面是代码:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lenovo.grideview">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@android:style/Theme.Black.NoTitleBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lenovo.grideview.MainActivity">
<!--
android:numColumns="3" 每行显示3列
android:horizontalSpacing="10dp"水平方向间距10dp
android:verticalSpacing="10dp" 竖直方向间距10dp
-->
<GridView
android:layout_marginTop="10dp"
android:id="@+id/grideview"
android:numColumns="3"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</GridView>
</LinearLayout>
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:gravity="center"
android:background="#000000"
android:orientation="vertical"
>
<ImageView
android:id="@+id/image"
android:layout_width="60dp"
android:layout_height="60dp"
/>
<TextView
android:layout_marginTop="5dp"
android:id="@+id/text"
android:text="文字"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
/>
</LinearLayout>
MainActivity.java
package com.example.lenovo.grideview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends Activity implements AdapterView.OnItemClickListener{
private GridView gridView;
private List<Map<String,Object>> datalist;
private SimpleAdapter adapter;
private int []icon= {
R.drawable.address_book,
R.drawable.calendar,
R.drawable.camera,
R.drawable.clock,
R.drawable.games_control,
R.drawable.messenger,
R.drawable.ringtone,
R.drawable.settings,
R.drawable.speech_balloon,
R.drawable.weather,
R.drawable.world,
R.drawable.youtube
};
private String[] iconName={
"通讯录","日历","照相机","时钟","游戏","短信",
"铃声","设置","语音","天气","浏览器","视频"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView=(GridView) findViewById(R.id.grideview);
/*
1.数据源
2.加载适配器
3.给GridView添加适配
4.给GridView视图添加点击事件(OnItemClickListener)
*/
datalist=new ArrayList<Map<String,Object>>();
adapter=new SimpleAdapter(this,getdata(),R.layout.item,new String[]{"image","text"},new int[]{R.id.image,R.id.text});
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this);
}
List<Map<String,Object>> getdata(){
for(int i=0;i<icon.length;i++){
Map<String,Object>map=new HashMap<String,Object>();
map.put("image",icon[i]);
map.put("text",iconName[i]);
datalist.add(map);
}
return datalist;
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(this,"我是"+iconName[i],Toast.LENGTH_SHORT).show();
}
}