首页 > 代码库 > Android-GridView网格试图Demo

Android-GridView网格试图Demo

技术分享

 

代码

package com.lxt008;import com.lxt008.R;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.GridView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;public class Activity01 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 = (GridView) findViewById(R.id.gridview);        //添加元素给gridview        gridview.setAdapter(new ImageAdapter(this));        // 设置Gallery的背景        gridview.setBackgroundResource(R.drawable.bg0);        //事件监听        gridview.setOnItemClickListener(new OnItemClickListener() {            public void onItemClick(AdapterView<?> parent, View v, int position, long id)            {                Toast.makeText(Activity01.this, "你选择了" + (position + 1) + " 号图片", Toast.LENGTH_SHORT).show();            }        });    }}

 

布局文件

<?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:numColumns="auto_fit"    android:verticalSpacing="10dp"    android:horizontalSpacing="10dp"    android:columnWidth="90dp"    android:stretchMode="columnWidth"    android:gravity="center"/>

 

Android-GridView网格试图Demo