Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android開發入門(十四)顯示圖像 14.3 GridView

Android開發入門(十四)顯示圖像 14.3 GridView

編輯:Android開發教程

GridView是一個可滑動的二維平面視圖。通常,用GridView去顯示一些圖片。下面展示如何使用GridView 。

1. 創建一個工程,Grid。

2. 在res/drawable-mdpi下面放一些圖片。

3. main.xml 中的代碼。

<?xml version="1.0" encoding="utf-8"?>    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" >    
       
<GridView  
    android:id="@+id/gridview" 
    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" />    
       
</LinearLayout>

4. GridActivity.java中的代碼。

public class GridActivity 

extends Activity {    
    //---the images to display---    
    Integer[] imageIDs = {    
            R.drawable.pic1,    
            R.drawable.pic2,    
            R.drawable.pic3,    
            R.drawable.pic4,    
            R.drawable.pic5,    
            R.drawable.pic6,    
            R.drawable.pic7    
    };    
       
    /** 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(getBaseContext(),    
                        "pic" + (position + 1) + " selected",    
                        Toast.LENGTH_SHORT).show();    
            }    
        });    
       
    }    
           
    public class ImageAdapter extends BaseAdapter     
    {    
        private Context context;    
       
        public ImageAdapter(Context c)    
        {    
            context = c;    
        }    
       
        //---returns the number of images---    
        public int getCount() {    
            return imageIDs.length;    
        }    
       
        //---returns the item---    
        public Object getItem(int position) {    
            return position;    
        }    
       
        //---returns the ID of an item---    
        public long getItemId(int position) {    
            return position;    
        }    
       
        //---returns an ImageView view---    
        public View getView(int position, View convertView,    
        ViewGroup parent)    
        {    
            ImageView imageView;    
            if (convertView == null) {    
                imageView = new ImageView(context);    
                imageView.setLayoutParams(new 
                    GridView.LayoutParams(85, 85));    
                imageView.setScaleType(    
                    ImageView.ScaleType.CENTER_CROP);    
                imageView.setPadding(5, 5, 5, 5);    
            } else {    
                imageView = (ImageView) convertView;    
            }    
            imageView.setImageResource(imageIDs[position]);    
            return imageView;    
        }    
    }    
       
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved