main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="300dip"
android:layout_height="300dip"
android:background="#ffffff"
android:orientation="vertical"
tools:context=".GridViewScrollActivity" >
<HorizontalScrollView
android:id="@+id/hsView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#E9E9E9" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<GridView
android:id="@+id/house_gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</GridView>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="50dip"
android:layout_height="50dip"
android:paddingBottom="3dip"
android:paddingTop="10dip"
android:textColor="#000000"
android:textSize="15px" />
GridViewScrollActivity.java
package com.shy;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout.LayoutParams;
import com.esri.android.map.MapView;
public class GridViewScrollActivity extends Activity {
MapView mMapView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMapView = new MapView(this);
GridView gridView = (GridView) findViewById(R.id.house_gridview);
List<String> list = new ArrayList<String>();
for (int i = 0; i < 100; i++) {
list.add(i + "");
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
GridViewScrollActivity.this, R.layout.text_view, list);
LayoutParams params = new LayoutParams(10 * (50 + 4),
LayoutParams.WRAP_CONTENT);
gridView.setLayoutParams(params); // gridView的整体宽度
gridView.setColumnWidth(50); // 列宽(像素)
gridView.setHorizontalSpacing(4); // 水平间距
gridView.setStretchMode(GridView.NO_STRETCH); // 不拉伸
gridView.setNumColumns(10); // 一排显示几列
gridView.setAdapter(adapter);
}
}