android 布局使用GridView

 

效果图:

                    

 1  <!-- stretchMode属性值的作用是设置GridView中的条目以什么缩放模式去填充空间。 -->
 2 <com.annie.setui.MyGridView
 3                         android:id="@+id/gridView"
 4                         android:layout_width="fill_parent"
 5                         android:layout_height="wrap_content"
 6                         android:background="#F5F5F5"
 7                         android:horizontalSpacing="0.0dip"
 8                         android:listSelector="@null"
 9                          android:numColumns="4"
10                         android:scrollbars="none"
11                         android:stretchMode="columnWidth"
12                          android:verticalSpacing="0.0dip" />
xml布局
 1 import android.content.Context;
 2 import android.util.AttributeSet;
 3 import android.widget.GridView;
 4 /**
 5  * @Description:解决在scrollview中只显示第一行数据的问题
 6  * @author http://blog.csdn.net/finddreams
 7  */ 
 8 public class MyGridView extends GridView{
 9     public MyGridView(Context context, AttributeSet attrs) {
10         super(context, attrs);
11     }
12 
13     public MyGridView(Context context) {
14         super(context);
15     }
16 
17     public MyGridView(Context context, AttributeSet attrs, int defStyle) {
18         super(context, attrs, defStyle);
19     }
20 
21     @Override
22     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
23         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
24                 MeasureSpec.AT_MOST);
25         super.onMeasure(widthMeasureSpec, expandSpec);
26     }
27 }
 1 private String onePersonCenter = "title1",twoAssign = "title2",threeSTMM = "title3",
 2             fourHtml = "title4",fiveRedPacket = "title5",sixPromotional = "title6",
 3             sevenNewCampain = "title7",eightBorrowing = "title8",ninePrivate = "title9",
 4             tenAboutUs = "title11",elevenSH = "title12";
 5 
 6 
 7 //适配器
 8 SimpleAdapter adapter = new SimpleAdapter(this, getData(), R.layout.grid_item_wallet, 
 9                 new String[]{"ItemImage","ItemText"}, new int[]{R.id.grid_img,R.id.grid_tv});
10 gridView.setAdapter(adapter);
11 
12 //数据源
13 private ArrayList<HashMap<String,Object>> getData(){  
14         String[] title = {onePersonCenter,twoAssign,threeSTMM,fourHtml,fiveRedPacket,
15                 sixPromotional,sevenNewCampain,eightBorrowing,ninePrivate,tenAboutUs,elevenSH};
16         int[] pic = {R.drawable.icon_of_grid1,R.drawable.icon_of_grid2,R.drawable.icon_of_grid3,
17         R.drawable.icon_of_grid4,R.drawable.icon_of_grid5,R.drawable.icon_of_grid6,
18         R.drawable.icon_of_grid7,R.drawable.icon_of_grid8,R.drawable.icon_of_grid9,
19         R.drawable.icon_of_grid10,R.drawable.icon_of_grid11};
20         
21         ArrayList<HashMap<String,Object>> sourceList = new ArrayList<HashMap<String,Object>>();  
22         HashMap<String, Object> tempHashMap = new HashMap<String, Object>();  
23         for (int i = 0; i < title.length; i++) { 
24             tempHashMap.put("ItemText", title[i]);
25             for (int j = 0; j < pic.length; j++) {
26                 tempHashMap.put("ItemImage", pic[i]);
27             }
28               
29             sourceList.add(tempHashMap);  
30             tempHashMap = new HashMap<String, Object>();  
31         }  
32         return sourceList;  
33           
34           
35           
36     }
activity
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent" 
 4     android:layout_margin="0.0dip">
 5     
 6     <RelativeLayout 
 7         android:layout_width="match_parent"
 8         android:layout_height="match_parent"
 9         android:paddingTop="20dip"
10         android:paddingBottom="20dip"
11         android:layout_centerInParent="true"
12         android:background="@drawable/bg_gv">
13 
14         <ImageView
15             android:id="@+id/grid_img"
16             android:layout_width="30dip"
17             android:layout_height="30dip"
18             android:layout_centerHorizontal="true"
19             android:scaleType="fitXY"/>
20 
21         <TextView
22             android:id="@+id/grid_tv"
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content"
25             android:paddingTop="5dip"
26             android:layout_centerHorizontal="true"
27             android:layout_below="@+id/grid_img"
28             android:textColor="@color/default_Tab3font"
29             android:text="11" />
30     </RelativeLayout>   
31 
32 </RelativeLayout>
xml item布局
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <selector xmlns:android="http://schemas.android.com/apk/res/android" >
 3     <item android:state_pressed="true"><shape android:shape="rectangle">
 4             <stroke android:width="1.0px" android:color="@color/gray_lines" />
 5 
 6             <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
 7         </shape></item>
 8     <item android:state_focused="true"><shape android:shape="rectangle">
 9             <gradient android:angle="270.0" android:endColor="#ffe8ecef" android:startColor="#ffe8ecef" />
10 
11             <stroke android:width="1.0px" android:color="@color/gray_lines" />
12         </shape></item>
13     <item><shape android:shape="rectangle">
14             <gradient android:angle="270.0" android:endColor="#ffffffff" android:startColor="#ffffffff" />
15 
16             <stroke android:width="1.0px" android:color="@color/gray_lines" />
17         </shape></item>
18 
19 </selector>
20 
21 // <color name="gray_lines">#e6e5e5</color>
bg_gv.xml

 

posted @ 2017-03-26 13:59  annie-baby  阅读(408)  评论(0编辑  收藏  举报