Print? <P> the grid view control enables multiple specified images to be displayed in the specified size. </P> <P> 1. layout file grid_view.xml </P>
The grid view control enables multiple specified images to be displayed in the specified size. 1. layout file 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. program file 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 (8, 85 ));
ImageView. setScaleType (ImageView. ScaleType. CENTER_CROP );
ImageView. setPadding (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 (8, 85 ));
ImageView. setScaleType (ImageView. ScaleType. CENTER_CROP );
ImageView. setPadding (8, 8, 8 );
} Else {
ImageView = (ImageView) convertView;
}
ImageView. setImageResource (mThumbIds [position]);
Return imageView;
}
}
}