关闭

Android布局之GridView使用展示

标签: gridview布局
200人阅读 评论(0) 收藏 举报
分类:

GridView使用ListAdapter把产生的元素自动驾到布局当中,ListAdapter有个子类是BaseAdapter

.XML中的代码如下

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android_gridview.MainActivity" >

    <GridView
         android:id="@+id/gridview"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:columnWidth="90dp"
         android:gravity="center"
         android:horizontalSpacing="10dp"
         android:numColumns="auto_fit"
         android:stretchMode="columnWidth"
         android:verticalSpacing="10dp"
        />
        
</RelativeLayout>

.java中的代码如下

package com.example.android_gridview;

import android.support.v7.app.ActionBarActivity;
import android.app.ActionBar.LayoutParams;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

	private GridView gridView;
	private ImageAdapter imageAdapter;//自定义的适配器

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		gridView = (GridView) this.findViewById(R.id.gridview);
		
		// 需要设置适配器,所以声明一个适配器,供使用
		imageAdapter=new ImageAdapter();
		gridView.setAdapter(imageAdapter);
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "点击了"+position, 1).show();
			}
		});

	}

	//图片资源,还可以用异步任务下载图片,放到此处
	private int[] images = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
			R.drawable.p4, R.drawable.p5, R.drawable.p6, R.drawable.p7,
			R.drawable.p8 };

	// 声明一个适配器
	public class ImageAdapter extends BaseAdapter {

		public ImageAdapter() {
			// TODO Auto-generated constructor stub
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return images.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return images[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		/**
		 * 在这里面重绘布局,有两种方法:
		 * 第一种,一般控件比较简单的就手工创建View的对象,第二种,图文混排,美化布局就要加载XML文件
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView imageView=null;
			if(convertView==null){
				imageView=new ImageView(MainActivity.this);
			}else{
				imageView=(ImageView) convertView;
			}
			/**
			 * 设置GridView的显示的格子的间距
			 * 
			 */
			//这句话写错,会出现闪退的情况,LayoutParams(100,100)里面的参数是设置图片在布局中显示的大小
			imageView.setLayoutParams(new GridView.LayoutParams(200,200));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setPadding(8, 8, 8, 8);
			//加载图片的三种方式:drawable,BitMap,从资源文件中取
			imageView.setImageResource(images[position]);
			return imageView;
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

运行效果图如下


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:50059次
    • 积分:1315
    • 等级:
    • 排名:千里之外
    • 原创:84篇
    • 转载:4篇
    • 译文:0篇
    • 评论:4条