Android 中的 Gridview 相簿
Android簡介使用方法開發工具開始寫程式視覺化介面視覺元件對話元件核心物件事件處理資料儲存查詢功能影音功能繪圖功能網路功能衛星地圖特殊功能資源管理裝置管理系統核心問題與回答刷機升級常用軟體Eclipse教學錄影訊息相關網站參考文獻最新修改簡體版English |
檔案下載:HelloGridView.zip 簡介Android 中的 GridView 是一種像相簿一樣的複雜型元件,您可以使用 XML 的語法描述介面,然後透過下列指令設定 GridView 的 Adapter。
ImageAdapter 當中使用了 ImageView 作為真正的顯示元件,在範例中我們透過下列的 setImageResource() 函數設定顯示的圖片群,然後就可以真正顯示在畫面上了。
程式範例檔案:res/layout/main.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:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center" /> 檔案:src/HelloGridView.java package ccc.test; import android.app.Activity; import android.os.Bundle; import android.view.*; import android.widget.*; import android.widget.AdapterView.OnItemClickListener; public class HelloGridView extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this)); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View v, int position, long id) { Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show(); } }); } } 檔案:src/ImageAdapter.java package ccc.test; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } // create a new ImageView for each item referenced by the Adapter public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // if it's not recycled, initialize some attributes 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; } // references to our images 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 }; } 執行結果圖一、範例 HelloGridView 的執行結果 授權聲明本文修改自 Android Developers 網站中的 HelloGridView 範例,其網址如下,使用時請遵守 Creative Commons Attribution 2.5 授權。
|
page revision: 6, last edited: 13 Oct 2010 00:58
Post preview:
Close preview