GridView使用

来源:互联网 发布:软件测试工程师自学 编辑:程序博客网 时间:2024/04/30 16:36

本文需要添加/修改3个文件:main.xml、night_item.xml、JAVA源代码。

main.xml源代码如下,本身是个GirdView,用于装载Item:

[xhtml] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <GridView xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:id="@+id/gridview"  
  4.     android:layout_width="fill_parent"   
  5.     android:layout_height="fill_parent"  
  6.     android:numColumns="auto_fit"  
  7.     android:verticalSpacing="10dp"  
  8.     android:horizontalSpacing="10dp"  
  9.     android:columnWidth="90dp"  
  10.     android:stretchMode="columnWidth"  
  11.     android:gravity="center"  
  12. />  

介绍一下里面的某些属性:

android:numColumns="auto_fit" ,GridView的列数设置为自动

android:columnWidth="90dp",每列的宽度,也就是Item的宽度
android:stretchMode="columnWidth",缩放与列宽大小同步
android:verticalSpacing="10dp",两行之间的边距,如:行一(NO.0~NO.2)与行二(NO.3~NO.5)间距为10dp
android:horizontalSpacing="10dp",两列之间的边距。

 

接下来介绍 night_item.xml,这个XML跟前面ListView的ImageItem.xml很类似:

[xhtml] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout   
  3.          xmlns:android="http://schemas.android.com/apk/res/android"   
  4.          android:layout_height="wrap_content"   
  5.          android:paddingBottom="4dip" android:layout_width="fill_parent">  
  6.          <ImageView   
  7.                android:layout_height="wrap_content"   
  8.                android:id="@+id/ItemImage"   
  9.                android:layout_width="wrap_content"   
  10.                android:layout_centerHorizontal="true">   
  11.          </ImageView>  
  12.          <TextView   
  13.                android:layout_width="wrap_content"   
  14.                android:layout_below="@+id/ItemImage"   
  15.                android:layout_height="wrap_content"   
  16.                android:text="TextView01"   
  17.                android:layout_centerHorizontal="true"   
  18.                android:id="@+id/ItemText">  
  19.          </TextView>  
  20. </RelativeLayout>  

 

最后就是JAVA的源代码了,也跟前面的ListView的JAVA源代码很类似,不过多了“选中”的事件处理:

[java] view plaincopyprint?
  1.   public void onCreate(Bundle savedInstanceState) {  
  2.       super.onCreate(savedInstanceState);  
  3.       setContentView(R.layout.main);  
  4.       GridView gridview = (GridView) findViewById(R.id.gridview);  
  5.         
  6.       //生成动态数组,并且转入数据  
  7.       ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();  
  8.       for(int i=0;i<10;i++)  
  9.       {  
  10.         HashMap<String, Object> map = new HashMap<String, Object>();  
  11.         map.put("ItemImage", R.drawable.icon);//添加图像资源的ID  
  12.     map.put("ItemText""NO."+String.valueOf(i));//按序号做ItemText  
  13.         lstImageItem.add(map);  
  14.       }  
  15.       //生成适配器的ImageItem <====> 动态数组的元素,两者一一对应  
  16.       SimpleAdapter saImageItems = new SimpleAdapter(this//没什么解释  
  17.                                                 lstImageItem,//数据来源   
  18.                                                 R.layout.night_item,//night_item的XML实现  
  19.                                                   
  20.                                                 //动态数组与ImageItem对应的子项          
  21.                                                 new String[] {"ItemImage","ItemText"},   
  22.                                                   
  23.                                                 //ImageItem的XML文件里面的一个ImageView,两个TextView ID  
  24.                                                 new int[] {R.id.ItemImage,R.id.ItemText});  
  25.       //添加并且显示  
  26.       gridview.setAdapter(saImageItems);  
  27.       //添加消息处理  
  28.       gridview.setOnItemClickListener(new ItemClickListener());  
  29.   }  
  30.     
  31.   //当AdapterView被单击(触摸屏或者键盘),则返回的Item单击事件  
  32.   class  ItemClickListener implements OnItemClickListener  
  33.   {  
  34. public void onItemClick(AdapterView<?> arg0,//The AdapterView where the click happened   
  35.                                   View arg1,//The view within the AdapterView that was clicked  
  36.                                   int arg2,//The position of the view in the adapter  
  37.                                   long arg3//The row id of the item that was clicked  
  38.                                   ) {  
  39.     //在本例中arg2=arg3  
  40.     HashMap<String, Object> item=(HashMap<String, Object>) arg0.getItemAtPosition(arg2);  
  41.     //显示所选Item的ItemText  
  42.     setTitle((String)item.get("ItemText"));  
  43. }  
  44.       
  45.   }  

http://blog.csdn.net/hellogv/article/details/4567095


刚刚学习Android的时候,GridView上元素的背景把我搞得很狼狈,那个背景的大小难以控制,导致一个元素背景经常会覆盖到相邻的元素.我费了好大力气才把GridView调整好,但是元素的背景依然没有好的办法去调整.

    使用<selector>风格化Android的GridView元素背景
    我所说的元素背景就是上面图片中Car Home图标后面的桔红色的方块.

    今天看了一段代码,是SDK/Sample/HOME项目,大家可以研究一下.

    在布局文件home.xml中有下面的代码:

    <GridView android:id="@+id/all_apps"
        android:background="@drawable/application_background"
        android:persistentDrawingCache="animation|scrolling"
        android:alwaysDrawnWithCache="true"
        android:scrollbars="none"
        android:drawSelectorOnTop="false"
        android:listSelector="@drawable/grid_selector"
        android:numColumns="auto_fit"
        android:columnWidth="78dp"
        android:stretchMode="spacingWidth"
        android:layout_weight="1.0"
        android:layout_height="0dip"
        android:layout_width="match_parent"
        android:stackFromBottom="true"
        android:visibility="invisible" 
    />
    在GridView属性中注意上面绿色的那一行.它指定了元素的背景布局文件为dwawable目录下面的grid_selector.xml文件.打开grid_selector.xml文件,有下面的描述:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" 

            android:drawable="@drawable/pressed_application_background_static" />
        <item android:state_window_focused="false"  

             android:drawable="@drawable/focused_application_background_static" />
        <item android:state_window_focused="true" 

              android:drawable="@drawable/focused_application_background_static" />
    </selector>

    在此文件中,三个<item>分别指定了在点击图标,聚焦,失焦的情况下使用的元素背景

    其中,在dwawable-hdpi目录下的pressed_application_background_static.png文件显示为:

        使用<selector>风格化Android的GridView元素背景

    在dwawable-hdpi目录下的focused_application_background_static文件显示为: 

      使用<selector>风格化Android的GridView元素背景
    这就是我们点击图标元素和滚动图标元素时元素后面的背景.我们通过这样的方法就可以改变其大小,形状以及颜色了,这会使你的GridView制作得更漂亮.


0 0
原创粉丝点击