Android GridView的使用

jopen 12年前

GridView就是先将控件横向显示,然后纵向显示

Android GridView的使用

效果如上所示

先看一下GridView的布局文件

<?xml version="1.0" encoding="utf-8"?>        <GridView         xmlns:android="http://schemas.android.com/apk/res/android"          android:id="@+id/brainheroall"          android:background="#FFeff1ef"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:numColumns="auto_fit"         android:verticalSpacing="10dp"         android:horizontalSpacing="10dp"         android:columnWidth="90dp"         android:stretchMode="columnWidth"         android:gravity="center"        />      


这是如何显示GridView,下面的xml文件是GridView里面如何显示

<?xml version="1.0" encoding="utf-8"?>    <RelativeLayout              xmlns:android="http://schemas.android.com/apk/res/android"              android:layout_height="wrap_content"              android:paddingBottom="4dip" android:layout_width="fill_parent">             <ImageView                    android:layout_height="wrap_content"                    android:id="@+id/ItemImage"                    android:layout_width="wrap_content"                    android:layout_centerHorizontal="true">              </ImageView>             <TextView                    android:layout_width="wrap_content"                    android:layout_below="@+id/ItemImage"                    android:layout_height="wrap_content"                    android:text=""                    android:textColor="#FF000000"                 android:layout_centerHorizontal="true"                    android:id="@+id/ItemText">             </TextView>    </RelativeLayout> 


下面是Java代码如何使用GridView

package view;    import java.util.ArrayList;  import java.util.HashMap;    import model.HeroSimpleInfo;  import service.ObtainInfo;  import android.app.Activity;  import android.content.Intent;  import android.os.Bundle;  import android.view.KeyEvent;  import android.view.View;  import android.widget.AdapterView;  import android.widget.AdapterView.OnItemClickListener;  import android.widget.GridView;  import android.widget.SimpleAdapter;  import android.widget.Toast;    import com.zhychneg.dota.R;    public class BrainHeroActivity extends Activity implements OnItemClickListener{     GridView gv=null;   long exitTime=0;   HeroSimpleInfo []heros=null;   @Override   protected void onCreate(Bundle savedInstanceState) {    // TODO Auto-generated method stub    super.onCreate(savedInstanceState);    setContentView(R.layout.brainheroall);    gv=(GridView) findViewById(R.id.brainheroall);            ArrayList<HashMap<String,Object>> al=new ArrayList<HashMap<String,Object>>();           heros=ObtainInfo.getBrainHeroSimpleInfo();            for(int i=0;i<heros.length;i++)    {     HashMap<String, Object> map = new HashMap<String, Object>();                        map.put("icon", heros[i].getRes());//添加图像资源的ID             map.put("name", heros[i].getName());//按序号做ItemText             al.add(map);      }        SimpleAdapter sa=new SimpleAdapter(this,al,R.layout.gridview_layout,new String[]{"icon","name"},new int[]{R.id.ItemImage,R.id.ItemText});    gv.setAdapter(sa);    gv.setOnItemClickListener(this);                           }      public void onItemClick(AdapterView<?> parent, View view, int position,     long id) {        Intent intent =new Intent(this,AboutHeroActivty.class);    intent.putExtra("HERO", heros[position]);    this.startActivity(intent);       }       }