程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 使用網格視圖控件GridView

使用網格視圖控件GridView

編輯:C#入門知識

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;
  }
 }
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved