日日行,不怕千万里;常常做,不怕千万事。 ——《格言联璧·处事》
本讲内容:GridView 网格组件
GridView是制作二维布局界面(行、列分布)
数据源(集合)-->适配器(SimpleAdapter)-->视图界面(GridView)
一、 GridView属性
属性名称 | 描述 |
android:columnWidth | GridView中每一列的宽度 |
android:gravity | 控件上面信息的位置 |
android:numColumns | 设置GridView的列数 |
android:horizontalSpacing | 设置水平之间的间距 |
android:verticalSpacing | 设置垂直之间的间距 |
示例一:
下面是res/layout/activity_main.xml 布局文件:
<RelativeLayout 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:padding="10dp"
android:background="@drawable/bg">
<GridView
android:id="@+id/id_gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:numColumns="3"
android:verticalSpacing="20dp"
android:horizontalSpacing="20dp"
android:gravity="center"/>
</RelativeLayout>
下面是res/layout/grid_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/id_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/id_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="名字"
android:textSize="18sp"
android:textColor="#ffffff" />
</LinearLayout>
下面是MainActivity.java主界面文件:
public class MainActivity extends Activity implements OnItemClickListener{
private GridView gridView;
private List<Map<String,Object>> dataList;
private SimpleAdapter adapter;
int[] drawable={ 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};
String[] iconName={ "联系人", "日历", "照相机", "时钟", "游戏", "短信", "铃声", "设置","语音", "天气", "浏览器", "视频" };
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
/**
* 初始化控件
*/
private void initViews() {
gridView=(GridView) findViewById(R.id.id_gridview);
dataList=new ArrayList<Map<String,Object>>();
adapter=new SimpleAdapter(this, getDate(), R.layout.grid_item,
new String[]{"image","name"}, new int[]{R.id.id_image,R.id.id_name});
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(this);
}
/**
* 数据源
*/
private List<Map<String, Object>> getDate(){
for(int i=0;i<drawable.length;i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", drawable[i]);
map.put("name", iconName[i]);
dataList.add(map);
}
return dataList;
}
/**
* 点击事件
*/
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Toast.makeText(this, "我是"+iconName[position], Toast.LENGTH_LONG).show();
}
}
Take your time and enjoy it 要原码的、路过的、学习过的请留个言,顶个呗~~