最近一個項目要用到Gallery的效果 由於業務的需要,必須要有GridView的功能。
- package com.easymorse.grid.demo;
-
- import java.util.ArrayList;
- import java.util.HashMap;
-
- import Android.app.ListActivity;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.GridView;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
-
- public class GridDemoActivity extends ListActivity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- LayoutInflater layoutInflater = (LayoutInflater) this
- .getSystemService("layout_inflater");
- View headerView=layoutInflater.inflate(R.layout.list_header, null);
- setGridView(headerView);
- ListView listView=(ListView) this.findViewById(android.R.id.list);
- listView.addHeaderView(headerView);
- listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,new String[]{"","","","","",""}));
- }
-
- private void setGridView(View view) {
- GridView gridView = (GridView) view.findViewById(R.id.grid);
- gridView.setNumColumns(10);
-
- ArrayList<HashMap<String, Object>> items = new ArrayList<HashMap<String, Object>>();
-
- for (int i = 0; i < 10; i++) {
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("ItemImage", R.drawable.k);
- map.put("ItemText", "" + "(" + i + ")");
- items.add(map);
- }
-
- SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.item,
- new String[] { "ItemImage", "ItemText" }, new int[] {
- R.id.ItemImage, R.id.ItemText });
- gridView.setAdapter(adapter);
- }
- }
item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:paddingBottom="10.0dip" android:layout_width="90.0dip"
- android:layout_height="140.0dip">
- <ImageView android:id="@+id/ItemImage" android:layout_width="80.0dip"
- android:layout_height="108.0dip" android:layout_marginLeft="10.0dip"
- android:layout_centerHorizontal="true">
- </ImageView>
- <TextView android:layout_below="@+id/ItemImage" android:id="@+id/ItemText"
- android:ellipsize="end" android:layout_width="80.0dip"
- android:layout_height="26.0dip" android:layout_marginTop="5.0dip"
- android:singleLine="true" android:layout_centerHorizontal="true">
- </TextView>
- </RelativeLayout>
list_header.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="200dp">
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="最近訪問人物" />
- <HorizontalScrollView android:layout_width="fill_parent"
- android:layout_height="160dp">
- <FrameLayout android:layout_width="fill_parent"
- android:layout_height="match_parent">
- <LinearLayout android:layout_width="1100dp"
- android:layout_height="match_parent" android:orientation="horizontal">
- <GridView android:id="@+id/grid" android:layout_width="fill_parent"
- android:gravity="center" android:layout_height="fill_parent"
- android:horizontalSpacing="1.0dip" android:verticalSpacing="1.0dip"
- android:stretchMode="spacingWidthUniform" android:numColumns="auto_fit"
- android:columnWidth="80dip">
- </GridView>
- </LinearLayout>
- </FrameLayout>
- </HorizontalScrollView>
- </LinearLayout>
main,xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="200dp">
- <TextView android:layout_width="fill_parent"
- android:layout_height="wrap_content" android:text="最近訪問人物" />
- <HorizontalScrollView android:layout_width="fill_parent"
- android:layout_height="160dp">
- <FrameLayout android:layout_width="fill_parent"
- android:layout_height="match_parent">
- <LinearLayout android:layout_width="1100dp"
- android:layout_height="match_parent" android:orientation="horizontal">
- <GridView android:id="@+id/grid" android:layout_width="fill_parent"
- android:gravity="center" android:layout_height="fill_parent"
- android:horizontalSpacing="1.0dip" android:verticalSpacing="1.0dip"
- android:stretchMode="spacingWidthUniform" android:numColumns="auto_fit"
- android:columnWidth="80dip">
- </GridView>
- </LinearLayout>
- </FrameLayout>
- </HorizontalScrollView>
- </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@android:id/list" android:cacheColorHint="#00000000"/>
</LinearLayout>