<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:columnWidth="90dp" android:stretchMode="columnWidth" android:gravity="center" android:numColumns="3" />
grid.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="4dip" android:layout_width="fill_parent"> <ImageView android:layout_height="wrap_content" android:id="@+id/ItemImage" android:layout_width="wrap_content" android:layout_centerHorizontal="true"> </ImageView> <TextView android:layout_width="wrap_content" android:layout_below="@+id/ItemImage" android:layout_height="wrap_content" android:text="TextView01" android:layout_centerHorizontal="true" android:id="@+id/ItemText"> </TextView> </RelativeLayout>
package com.example.yanlei.my; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.SimpleAdapter; import java.util.ArrayList; import java.util.HashMap; public class MainActivity extends AppCompatActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridView gridview = (GridView) findViewById(R.id.gridview); //生成動態數組,並且轉入數據 ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>(); for (int i = 0; i < 10; i++) { HashMap<String, Object> map = new HashMap<String, Object>(); if (i == 0) { map.put("ItemImage", R.drawable.a1);//添加圖像資源的ID } else { map.put("ItemImage", R.drawable.ic_launcher);//添加圖像資源的ID } map.put("ItemText", "NO." + String.valueOf(i));//按序號做ItemText lstImageItem.add(map); } //生成適配器的ImageItem <====> 動態數組的元素,兩者一一對應 SimpleAdapter saImageItems = new SimpleAdapter(this, lstImageItem,//數據來源 R.layout.grid,//grid的XML實現 //動態數組與ImageItem對應的子項 new String[]{"ItemImage", "ItemText"}, //ImageItem的XML文件裡面的一個ImageView,兩個TextView ID new int[]{R.id.ItemImage, R.id.ItemText}); //添加並且顯示 gridview.setAdapter(saImageItems); //添加消息處理 gridview.setOnItemClickListener(new ItemClickListener()); } //當AdapterView被單擊(觸摸屏或者鍵盤),則返回的Item單擊事件 class ItemClickListener implements OnItemClickListener { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2); //顯示所選Item的ItemText setTitle((String) item.get("ItemText")); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }