android > layout >GridView (九宫格)
先是 mian.xml 这个是 布局 就是 GridView
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:columnWidth="90dp" android:stretchMode="columnWidth" android:scrollbars="vertical" android:scrollbarStyle="outsideInset" android:gravity="center" > </GridView>
然后是 list 内部的 布局 , main_list.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:id="@+id/ItemImage" android:layout_height="wrap_content" 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>
然后是 主 Activity
package sp.mft; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView; import android.widget.GridView; import android.widget.SimpleAdapter; public class SpActivity extends Activity { /** Called when the activity is first created. */ private GridView gridview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gridview = (GridView)findViewById(R.id.gridview); ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>(); for(int i=0;i<20;i++) { HashMap<String, Object> map = new HashMap<String, Object>(); map.put("ItemImage", R.drawable.ic_launcher);//添加图像资源的ID map.put("ItemText", "NO."+String.valueOf(i));//按序号做ItemText lstImageItem.add(map); } SimpleAdapter saImageItems = new SimpleAdapter(SpActivity.this,lstImageItem, R.layout.main_list, new String[]{"ItemImage","ItemText"}, new int[]{R.id.ItemImage,R.id.ItemText}); gridview.setAdapter(saImageItems); gridview.setOnItemClickListener(new ItemClickListener()); //\\ } class ItemClickListener implements OnItemClickListener{ public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2); setTitle((String)item.get("ItemText")); } } }
相关推荐
chenjinlong 2020-01-29
vickyjfr 2013-08-27
luoj 2011-08-17
huohu00 2015-04-22
omguare 2012-05-07
SuperMenyII 2019-06-26
AngelicaA 2011-05-02
AABBbaby 2018-07-27
DrZOU 2017-12-21
wuqiong 2017-04-10
Lzn0 2017-03-27
fengyeezju 2015-12-25
superxlcr 2015-05-27
rookieding 2015-04-22
familynt 2015-04-06
mishaki 2015-02-13
江南以北 2014-12-28