安卓gridview 網格,多行多列實現



主Activity()


private int[] image = { R.drawable.camera, R.drawable.wifi, R.drawable.temperature, R.drawable.lamp, R.drawable.wechat, R.drawable.mic, }; private String[] text = { "攝像頭", "網絡", "溫濕度", "電器", "微信", "語音" };
 //顯示GridView的界面
                GridView gridview = (GridView) findViewById(R.id.gridView1);
                ArrayList<HashMap<String, Object>> imagelist = new ArrayList<HashMap<String, Object>>();
                // 使用HashMap將圖片添加到一個數組中,注意一定要是HashMap<String,Object>類型的,因為裝到map中的圖片要是資源ID,而不是圖片本身
                  // 如果是用findViewById(R.drawable.image)這樣把真正的圖片取出來了,放到map中是無法正常顯示的
                for (int i = 0; i < 6; i++) {
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("image", image[i]);
                    map.put("text", text[i]);
                    imagelist.add(map);
                }
          
http://www.cnblogs.com/xiaobo-Linux/ qq463431476

                SimpleAdapter simpleAdapter = new SimpleAdapter(this, imagelist,
                      R.layout.items, new String[] { "image", "text" }, new int[] {
                              R.id.image, R.id.title });
                // 設置GridView的適配器為新建的simpleAdapter
              gridview.setAdapter(simpleAdapter);


主xml

    <GridLayout
            android:layout_width="match_parent"
            android:layout_height="83dp"
            android:columnCount="1" 
         >

        </GridLayout>

        <GridView
            android:id="@+id/gridView1"
            android:layout_width="match_parent"
            android:layout_height="53dp"
            android:layout_gravity="left|bottom"
            android:columnWidth="90dp"
            android:fadeScrollbars="true"
 
            android:numColumns="2"
            android:stretchMode="columnWidth" />

items.xml

<?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" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10px"
        android:scaleType="fitCenter" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="5px" />

</LinearLayout>

 

安卓中的GridView屬性大全

android:numColumns=”auto_fit” //GridView的列數設置為自動 android:columnWidth=”90dp " //每列的寬度,也就是Item的寬度 android:stretchMode=”columnWidth" //縮放與列寬大小同步 android:verticalSpacing=”10dp” //兩行之間的邊距 android:horizontalSpacing=”10dp” //兩列之間的邊距 android:cacheColorHint="#00000000" //去除拖動時默認的黑色背景 android:listSelector="#00000000" //去除選中時的黃色底色 android:scrollbars="none" //隱藏GridView的滾動條 android:fadeScrollbars="true" //設置為true就可以實現滾動條的自動隱藏和顯示 android:fastScrollEnabled="true" //GridView出現快速滾動的按鈕(至少滾動4頁才會顯示) android:fadingEdge="none" //GridView衰落(褪去)邊緣顏色為空,缺省值是vertical。(可以理解為上下邊緣的提示色) android:fadingEdgeLength="10dip" //定義的衰落(褪去)邊緣的長度 android:stackFromBottom="true" //設置為true時,你做好的列表就會顯示你列表的最下面 android:transcriptMode="alwaysScroll" //當你動態添加數據時,列表將自動往下滾動最新的條目可以自動滾動到可視范圍內 android:drawSelectorOnTop="false" //點擊某條記錄不放,顏色會在記錄的后面成為背景色,內容的文字可見(缺省為false)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM