GridView(网格视图)
先看效果:
实现:
package com.example.grideviewimpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
// V
private GridView gridview;
// M
private String[] titles = { "1", "2", "3", "4", "5", "6" };
private int[] images = { R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher };
private ArrayList<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
// C
private SimpleAdapter SimpleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 实例化V层
gridview = (GridView) findViewById(R.id.gv);
// 实例化适配器
SimpleAdapter = new SimpleAdapter(MainActivity.this, getData(),
R.layout.myitem, new String[]{"titles","images"}, new int[]{R.id.tv,R.id.iv});
// 载入Gridview
gridview.setAdapter(SimpleAdapter);
}
public List<Map<String, Object>> getData() {
for (int i = 0; i < titles.length; i++) {
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("images", images[i]);
map.put("titles", titles[i]);
this.list.add(map);
}
return list;
}
}
<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:orientation="vertical" tools:context="${relativePackage}.${activityClass}" > <GridView android:id="@+id/gv" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:numColumns="3" > </GridView></LinearLayout>
<?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"
android:gravity="center"
>
<ImageView
android:id="@+id/iv"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
/>
<TextView android:id="@+id/tv"
android:layout_width="50dp"
android:layout_height="30dp"
android:gravity="center"
/>
</LinearLayout>
先写代码吧,有时间再说详细点