您的位置:首页 > 其它

16.GridView宫格的实现及其显示其…

2015-08-02 12:59 387 查看
1.Gridview布局(控件)

 (1)xml布局中得实现:

            
android:id="@+id/gridview"

           
android:background="#f0f2f1"

           
android:layout_width="fill_parent"

           
android:layout_height="fill_parent"

           
android:columnWidth="90dp"

           
android:gravity="center"

           
android:horizontalSpacing="2dp"

           
android:numColumns="auto_fit"

           
android:stretchMode="columnWidth"

           
android:verticalSpacing="3dp"  "

 >

 

 

(2)GridView的item实现
http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"

   
android:layout_height="match_parent" >

   
       
android:id="@+id/itemImage"

       
android:layout_width="wrap_content"

       
android:layout_height="wrap_content"

       
android:layout_centerHorizontal="true" >

   

 

2.activity
的实现


//1.声明存放图片id和显示的名称

private String texts[] = null;

private int images[] = null;

 

//2.主要实现(在OnCreate()中实现)

     
//九宫格

  images = new int[] {
R.drawable.feizhi, R.drawable.suliao,

    R.drawable.jiaju,
R.drawable.jinshu, R.drawable.boli,

    R.drawable.buliao,
R.drawable.dianqi, R.drawable.qidai };

  texts = new String[] { "废纸系列",
"塑料系列", "家具系列", "金属系列", "玻璃系列", "布料系列",

    "电器系列",
"期待" };

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

  ArrayList> lstImageItem =
new ArrayList>();

  for (int i = 0; i < 8; i++)
{

   HashMap map =
new HashMap();

   map.put("itemImage",
images[i]);

   map.put("itemText",
texts[i]);

   lstImageItem.add(map);

  }

  SimpleAdapter saImageItems =
new SimpleAdapter(this, lstImageItem,// 数据源

    R.layout.grid_item,//
显示布局

    new
String[] { "itemImage" }, new int[] { R.id.itemImage });

  gridview.setAdapter(saImageItems);

  gridview.setOnItemClickListener(new
ItemClickListener());

(PS点击事件见下篇文章)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: