編輯:高級開發
新建一個HelloGridVIEw的工程
修改main.XML代碼如下:
XML代碼
< ?XML version="1.0" encoding="utf-8"?>
< GridVIEw
XMLns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridvIEw"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center">
< /GridVIEw>
< ?XML version="1.0" encoding="utf-8"?>
< GridVIEw
XMLns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridvIEw"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center">
< /GridVIEw>
很簡單,就是一個GridVIEw。設置列寬為90dp,這樣當我們的numColumns設置為auto_fit時,Android就會自動計算我們手機屏幕的大小以決定每一行展示幾個元素。這是很方便。android:horizontalSpacing定義列之間的間隔,android:verticalSpacing定義行之間的間隔。android:stretchMode設置為columnWidth是意味著根據列寬自動縮放。
修改我們Activity HelloGridVIEw如下:
Java代碼
/**
*
* @author 飛雪無情
* @since 2011-1-20
*/
public class HelloGridVIEw extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
接上頁
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
GridView gridView=(GridView)findViewById(R.id.gridvIEw);
gridVIEw.setAdapter(new ImageAdapter(this));
//單擊GridVIEw元素的響應
gridVIEw.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View vIEw,
int position, long id) {
//彈出單擊的GridVIEw元素的位置
Toast.makeText(HelloGridVIEw.this,mThumbIds[position], Toast.LENGTH_SHORT).show();
}
});
}
private class ImageAdapter extends BaseAdapter{
private Context mContext;
public ImageAdapter(Context context) {
this.mContext=context;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, VIEwGroup parent) {
//定義一個ImageView,顯示在GridVIEw裡
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;
}
}
//展示圖片
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
接上頁
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, 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,
R.drawable.sample_0, 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
};
}
/**
*
* @author 飛雪無情
* @since 2011-1-20
*/
public class HelloGridVIEw extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.main);
GridView gridView=(GridView)findViewById(R.id.gridvIEw);
gridVIEw.setAdapter(new ImageAdapter(this));
//單擊GridVIEw元素的響應
gridVIEw.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View vIEw,
int position, long id) {
//彈出單擊的GridVIEw元素的位置
Toast.makeText(HelloGridVIEw.this,mThumbIds[position], Toast.LENGTH_SHORT).show();
}
});
}
private class ImageAdapter extends BaseAdapter{
private Context mContext;
public ImageAdapter(Context context) {
this.mContext=context;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return mThumbIds[position];
}
@Override
public long getItemId(int position) {
接上頁
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, VIEwGroup parent) {
//定義一個ImageView,顯示在GridVIEw裡
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;
}
}
//展示圖片
private Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, 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,
R.drawable.sample_0, 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
};
}
這裡我們也是采用的自定義Adapter,可以上一節的自定義Adapter講解。展示了一些圖片,然後點擊一個圖片的時候會顯示這個圖片所在的位置。
由於目前基於android的手機價格比較貴。如果只是為了體驗android,花幾千元買個手機也不值得。不過幸好Google想到了這一點,剛剛推出了Android Li
建議首先閱讀下面兩篇文章,這樣才可以更好的理解Activity的加載模式: android的進程,線程模型: http://www.cnblogs.com/ghj1
android 3.0 Honeycomb(蜂巢)已經在2011年CES上發布,然後現在android 3.0 SDK最終預覽版也放了出來,過段時間2011年Googl
在前面已經學會了 Android 的意圖(Intent),這是落實意圖,即一個對象。來自一個部件的消息傳遞到另一個組件使用 - 在應用程序或應用程序之外。因此這裡不需要從