标签:android des style blog http color java io
GridView跟ListView的区别:
GridView只有一列的话就是一个ListView
simple:
cell.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/image1" android:layout_width="50dp" android:layout_height="50dp" android:contentDescription="@string/str_datu" /> </LinearLayout>
mail.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_horizontal" android:orientation="vertical" tools:context="com.crazy.gridview.MainActivity" > <GridView android:id="@+id/grid01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:horizontalSpacing="1pt" android:numColumns="4" android:verticalSpacing="1pt" /> <ImageView android:id="@+id/imageView" android:layout_width="240dp" android:layout_height="240dp" android:layout_gravity="center_horizontal" android:contentDescription="@string/string_yulan" /> </LinearLayout>
MainActivity.java:
public class MainActivity extends Activity { GridView m_gridView; ImageView m_imageView; int[] image_ids = new int[] { R.drawable.focus_1, R.drawable.focus_2, R.drawable.focus_3, R.drawable.focus_4, R.drawable.focus_5, R.drawable.focus_7, R.drawable.focus_8, R.drawable.focus_9, R.drawable.focus_10, R.drawable.focus_11, R.drawable.focus_12, R.drawable.focus_13, R.drawable.focus_14, R.drawable.focus_15, R.drawable.focus_16 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 创建list对象,存放的是map List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); for (int i = 0; i < image_ids.length; i++) { Map<String, Object> item = new HashMap<String, Object>(); item.put("image", image_ids[i]); listItems.add(item); } // 获取显示图片的imageview m_imageView = (ImageView) findViewById(R.id.imageView); // 创建一个SimpleAdapter SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItems, R.layout.cell, new String[] { "image" }, new int[] { R.id.image1 }); m_gridView = (GridView) findViewById(R.id.grid01); m_gridView.setAdapter(simpleAdapter); m_gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub m_imageView.setImageResource(image_ids[arg2]); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.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(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
标签:android des style blog http color java io
原文地址:http://www.cnblogs.com/houchuantong/p/3891244.html