GridViewDemo
package com.pc.jiyuan.viewadapter;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GridViewDemo extends Activity {
private List<Map<String, Object>> list;
int[] pic = new int[]{R.drawable.address_book, R.drawable.calendar,
R.drawable.camera, R.drawable.clock,
R.drawable.games_control, R.drawable.messenger,
R.drawable.ringtone, R.drawable.settings,
R.drawable.speech_balloon, R.drawable.weather,
R.drawable.world, R.drawable.youtube,
};
String[] str = new String[]{"通讯录", "日历",
"照相机", "闹钟",
"游戏", "信息",
"铃声", "设置",
"语音", "天气",
"世界", "视频",
};
private GridView gvView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//gridview布局
gvView = (GridView) findViewById(R.id.gv_view);
list = new ArrayList<Map<String, Object>>();
getData();
//适配器
String[] from = {"image", "text"};
// int [] to ={R.id.image,R.id.text};
int[] to = {R.id.iv_image, R.id.tv_text};
SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.item_gridview, from, to);
//显示
gvView.setAdapter(simpleAdapter);
}
private List<Map<String, Object>> getData() {
for (int i = 0; i < str.length; i++) {
Map<String, Object> map = new HashMap<>();
map.put("image", pic[i]);
map.put("text", str[i]);
list.add(map);
}
Log.d("GridViewDemo",""+list.size());
return list;
}
}
item
注意点:
如果item中用到 padding属性,如padding = 10dp,gridview中可以不用android:verticalSpacing="10dp"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<GridView
android:id="@+id/gv_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="10dp"
>
</GridView>
</LinearLayout>
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<GridView
android:id="@+id/gv_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="10dp"
>
</GridView>
</LinearLayout>
注意点:
布局知识点:
GirdView的一些属性:
android:numColumns="auto_fit" --------列数设置为自动
android:columnWidth="90dp",----------每列的宽度,也就是Item的宽度
android:stretchMode="columnWidth"------缩放与列宽大小同步
android:verticalSpacing="10dp"----------垂直边距
android:horizontalSpacing="10dp"-------水平边距
<GridView android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="35px" <!-- grid元素之间的竖直间隔 -->
android:horizontalSpacing="5px" <!--grid元素之间的水平间隔 -->
android:numColumns="auto_fit" <!--表示有多少列,如果设置为auto_fit,将根据columnWidth和Spacing来自动计算 -->
android:columnWidth="100px" <!-- 一般建议采用有像素密度无关的dip或者dp来表示-->
android:stretchMode="columnWidth" <!--如何填满空余的位置,模拟器采用WVGA800*480,每排4列,有4*100+5*3=415,还余65px的空间,如果是columnWidth,则这剩余的65将分摊给4列,每列增加16/17px。如果采用SpacingWidth,则分摊给3个间隔空隙 -->
android:gravity="center" />
ps:
gridview间隔太大,可以把columnWidth写小点
我的gridview布局
<com.ailisi.myn.widget.MyGridView
android:id="@+id/gv_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_user_comment"
android:columnWidth="45dp"
android:horizontalSpacing="3dp"
android:numColumns="auto_fit">
</com.ailisi.myn.widget.MyGridView>
item布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
>
<ImageView
android:id="@+id/iv_grid"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/red"
android:scaleType="fitXY"
/>
</LinearLayout>
参考链接:
http://blog.csdn.net/love_javc_you/article/details/44454395
http://www.cnblogs.com/tinyphp/p/3855224.html
间距参考链接 http://m.blog.csdn.net/article/details?id=42394221
代码下载:
https://yunpan.cn/cSRWCt9MQBdV8 访问密码 2a09
2、listview嵌套gridview http://www.eoeandroid.com/thread-567170-1-1.html