print?<P>網格視圖控件的功能使將很多張指定的圖片以指定的大小顯示出來。</P><P>1、布局文件grid_view.xml</P>
網格視圖控件的功能使將很多張指定的圖片以指定的大小顯示出來。1、布局文件grid_view.xml[html]
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
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:gravity="center"
/>
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grid_view"
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:gravity="center"
/>2、程序文件GridViewActivity.java
[html]
package com.example.chong;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class GridViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("ImageViewActivity");
setContentView(R.layout.grid_view);
GridView gridView = (GridView)findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mThumbIds = {
R.drawable.grid_view_01,R.drawable.grid_view_02,
R.drawable.grid_view_03,R.drawable.grid_view_04,
R.drawable.grid_view_05,R.drawable.grid_view_06,
R.drawable.grid_view_07,R.drawable.grid_view_08,
R.drawable.grid_view_09,R.drawable.grid_view_10,
R.drawable.grid_view_11,R.drawable.grid_view_12,
R.drawable.grid_view_13,R.drawable.grid_view_14,
R.drawable.grid_view_15,R.drawable.sample_1,
R.drawable.sample_2,R.drawable.sample_3,
R.drawable.sample_4,R.drawable.sample_5,
R.drawable.sample_6,R.drawable.sample_7
};
public ImageAdapter(Context c)
{
mContext=c;
}
public int getCount()
{
return mThumbIds.length;
}
public View getView(int position,View convertView,ViewGroup parent)
{
ImageView imageView;
if(convertView==null)
{
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85,85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
}else {
imageView = (ImageView)convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
}
package com.example.chong;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class GridViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("ImageViewActivity");
setContentView(R.layout.grid_view);
GridView gridView = (GridView)findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mThumbIds = {
R.drawable.grid_view_01,R.drawable.grid_view_02,
R.drawable.grid_view_03,R.drawable.grid_view_04,
R.drawable.grid_view_05,R.drawable.grid_view_06,
R.drawable.grid_view_07,R.drawable.grid_view_08,
R.drawable.grid_view_09,R.drawable.grid_view_10,
R.drawable.grid_view_11,R.drawable.grid_view_12,
R.drawable.grid_view_13,R.drawable.grid_view_14,
R.drawable.grid_view_15,R.drawable.sample_1,
R.drawable.sample_2,R.drawable.sample_3,
R.drawable.sample_4,R.drawable.sample_5,
R.drawable.sample_6,R.drawable.sample_7
};
public ImageAdapter(Context c)
{
mContext=c;
}
public int getCount()
{
return mThumbIds.length;
}
public View getView(int position,View convertView,ViewGroup parent)
{
ImageView imageView;
if(convertView==null)
{
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85,85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
}else {
imageView = (ImageView)convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
}