Android学习 18 ->网格控件GridView

     GridView是一项显示二维的viewgroup,可滚动的网格。一般用来显示多张图片。是一个在平面上可显示多个条目的可滚动的视图组件,该组件中的条目通过一个ListAdapter和该组件进行关联。其实其效果和GridLayout布局形式差不多。

   官方定义的xml属性:

属性名称

描述

android:columnWidth

设置列的宽度。关联的方法为:setColumnWidth(int)

android:gravity

设置此组件中的内容在组件中的位置。可选的值有:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical 可以多选,用“|”分开。关联方法:setGravity (int gravity)

android:horizontalSpacing

两列之间的间距。关联方法:setHorizontalSpacing(int)

android:numColumns

列数。关联方法:setNumColumns(int)

android:stretchMode

缩放模式。关联方法:setStretchMode(int)

android:verticalSpacing

两行之间的间距。关联方法:setVerticalSpacing(int)

    其中android:numColumns="  "设置列数,"  "中如果是制定的数字就是设置相对应的列数,而"  "中为auto_fit时则是系统根据你放置的内容来自动匹配列数。

 

     以下图所示界面为例:

Android学习 18 ->网格控件GridView_第1张图片

   所需要的代码以及xml布局如下:

    activity_auto_complet_textview_layout.xml布局:

<span style="font-size:18px;"><?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:id="@+id/my_gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:horizontalSpacing="5dp"
        android:numColumns="3"
        android:verticalSpacing="5dp" />

</LinearLayout></span>


    item_gridview.xml布局:

<span style="font-size:18px;"><?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"
    android:padding="5dp">

    <ImageView
        android:id="@+id/msg_grid_img"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:background="@drawable/ped" />

    <TextView
        android:id="@+id/msg_gridview_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="你好Android"
        android:layout_marginTop="5dp"
        android:textSize="12sp" />

</LinearLayout></span>


   其GridViewActivity代码中用的是之前设置的自定义适配器:

<span style="font-size:18px;">package com.sc.android.ui.gritview;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.sc.android.R;
import com.sc.android.activity.bean.InformationBean;

public class GridViewActivity extends Activity {

	private GridView mGridView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_gridview_layout);

		mGridView = (GridView) findViewById(R.id.gridview);

		GridAdapter adapter = new GridAdapter(this, getData());

		mGridView.setAdapter(adapter);
	}

	public List<InformationBean> getData() {
		List<InformationBean> list = new ArrayList<InformationBean>();
		InformationBean information = new InformationBean();
		information.setTitle("qwasfasfdsadt");
		information.setIcon(R.drawable.gyy1);
		list.add(information);

		information = new InformationBean();
		information.setTitle("asdfsdafsdafsdf");
		information.setIcon(R.drawable.ka1);
		list.add(information);

		information = new InformationBean();
		information.setTitle("sdfqwetsdagfdas");
		information.setIcon(R.drawable.nll1);
		list.add(information);

		information = new InformationBean();
		information.setTitle("adsfsdagdsagew");
		information.setIcon(R.drawable.nll2);
		list.add(information);

		information = new InformationBean();
		information.setTitle("asdfasdt3fsda");
		information.setIcon(R.drawable.qz1);
		list.add(information);
		information = new InformationBean();
		information.setTitle("qwasfasfdsadt");
		information.setIcon(R.drawable.gyy1);
		list.add(information);

		information = new InformationBean();
		information.setTitle("asdfsdafsdafsdf");
		information.setIcon(R.drawable.ka1);
		list.add(information);

		information = new InformationBean();
		information.setTitle("sdfqwetsdagfdas");
		information.setIcon(R.drawable.nll1);
		list.add(information);

		information = new InformationBean();
		information.setTitle("adsfsdagdsagew");
		information.setIcon(R.drawable.nll2);
		list.add(information);

		information = new InformationBean();
		information.setTitle("asdfasdt3fsda");
		information.setIcon(R.drawable.qz1);
		list.add(information);

		information = new InformationBean();
		information.setTitle("qwasfasfdsadt");
		information.setIcon(R.drawable.gyy1);
		list.add(information);

		information = new InformationBean();
		information.setTitle("asdfsdafsdafsdf");
		information.setIcon(R.drawable.ka1);
		list.add(information);

		information = new InformationBean();
		information.setTitle("sdfqwetsdagfdas");
		information.setIcon(R.drawable.nll1);
		list.add(information);

		information = new InformationBean();
		information.setTitle("adsfsdagdsagew");
		information.setIcon(R.drawable.nll2);
		list.add(information);

		information = new InformationBean();
		information.setTitle("asdfasdt3fsda");
		information.setIcon(R.drawable.qz1);
		list.add(information);

		return list;
	}

	public class GridAdapter extends BaseAdapter {

		private List<InformationBean> mList = new ArrayList<InformationBean>();;
		private LayoutInflater mInflater;

		public GridAdapter(Context context, List<InformationBean> list) {
			mList = list;
			mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ContentMessge content;
			if (convertView == null) {

				convertView = mInflater.inflate(R.layout.item_gridview, null);

				content = new ContentMessge();

				content.iconImg = (ImageView) convertView
						.findViewById(R.id.msg_grid_img);
				content.textTitle = (TextView) convertView
						.findViewById(R.id.msg_gridview_txt);

				convertView.setTag(content);
			} else {
				content = (ContentMessge) convertView.getTag();
			}

			InformationBean information = (InformationBean) getItem(position);
			content.iconImg.setBackgroundResource(information.getIcon());
			content.textTitle.setText(information.getTitle());

			return convertView;
		}
	}

	class ContentMessge {
		ImageView iconImg;
		TextView textTitle;
	}
}
</span>


     自定义的适配器在之前的内容中已经有,此处就不再重复代码了,当然也可以用系统的适配器实现。

 

你可能感兴趣的:(Android学习 18 ->网格控件GridView)