这个网格布局和之前的ListView列表(https://blog.csdn.net/qq_43145926/article/details/89403901 ) 布局差不多的意思:
1 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"
tools:context="${relativePackage}.${activityClass}"
android:background="#FF000000"
>
<GridView
android:id="@+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:columnWidth="80dp"
android:stretchMode="columnWidth"
android:verticalSpacing="30dp"
/>
RelativeLayout>
2 在新的xml中:
<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/fruit_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
/>
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#FFFFFFFF"
/>
LinearLayout>
3 新建一个class类
public class Fruit {//Fruit 类中只有两个字段,name 表示水果的名字,imageId 表示水果对应图片的资源 id
private String name;
private int imageId;
public Fruit(String name,int imageId){
this.name=name;
this.imageId=imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
4 新建一个数组适配器
public class FruitAdapter extends ArrayAdapter<Fruit> {
private int resourceId;
public FruitAdapter(Context context, int resource, List<Fruit> objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub
resourceId=resource;
}
//每次显示一个item都调用一次getview方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Fruit fruit = getItem(position); // 获取当前项的Fruit实例
View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
ImageView fruitImage = (ImageView) view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
}
5 在MainActivity中
public class MainActivity extends Activity {
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 = { "通讯录", "日 历", "照相机", "时 钟", "游 戏", "短 信", "铃 声",
"设 置", "语 音", "天 气", "浏览器", "视 频" };
private List<Fruit> fruitList = new ArrayList<Fruit>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<icon.length;i++){
fruitList.add(new Fruit(iconName[i], icon[i]));
}
// Fruit apple = new Fruit("Apple", R.drawable.ic_launcher);
// fruitList.add(apple);
// Fruit banana = new Fruit("Banana", R.drawable.ic_launcher);
// fruitList.add(banana);
// Fruit orange = new Fruit("Orange", R.drawable.ic_launcher);
// fruitList.add(orange);
FruitAdapter adapter = new FruitAdapter(MainActivity.this,R.layout.fruit_item, fruitList);
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "您选择了 "+iconName[position], 0).show();
}
});
}
}