Android_gridView应用实例

gridView的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <GridView 
        android:horizontalSpacing="2dp"
        android:verticalSpacing="2dp"
        android:columnWidth="100dp"
        android:numColumns="auto_fit"
        android:id="@+id/gView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        />
</LinearLayout>

对应的Activity代码

package com.tmacsky;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class MyGridViewActivity extends Activity {
    //定义图片数组
    private int[] mImages = new int[]{R.drawable.ha,R.drawable.hb,R.drawable.hc,R.drawable.hd,R.drawable.he,R.drawable.hf,R.drawable.hg,R.drawable.hh};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridview);
        GridView gView = (GridView)findViewById(R.id.gView);
        //建立数据源,设置adapter
        gView.setAdapter(new ImageAdapter());
    }
    class ImageAdapter extends BaseAdapter{
        public int getCount() {
            // TODO Auto-generated method stub
            return mImages.length;
        }
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return mImages[position];
        }
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return mImages[position];
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ImageView imageView = new ImageView(MyGridViewActivity.this);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setPadding(5, 5, 5, 5);
            imageView.setImageResource(mImages[position]);
            return imageView;
        }
    }
}

摘自网址:http://www.cnblogs.com/snake-hand/p/3188452.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值