1.GridView简介
GridView是ViewGroup子类,主要用于显示在一个二维可滚动的网格,比如九宫格。使用gridview和listview类似,都是使用ListAdapter来填充数据加载布局。
2.XML Attributes
(1).android:columnWidth
主要用来指定每一列的宽度,使用该属性时应该注意和后面的拉伸模式相关联。如果拉伸模式为spacingWidth,则该属性必须指定;如果拉伸模式为columnWidth指定该属性无效。
(2).android:gravity
用于指定每个item的对其方式起始位置,当有多个是可以使用'|'隔开
(3).android:horizontalSpacing
用于指定每一列的之间的间距。注意:如果拉伸模式为spacingWidth,则该值设定无效
(4).android:numColumns
每一列的item数目
(5).android:verticalSpacing
用于指定每一行的之间的间距。
(6).android:stretchMode(***)
如果以列间距拉伸,则不需要指定列之间的间距;如果以列的等宽度拉伸,则不需要指定列的宽度。
常量
值
描述
none
0
拉伸被禁用,不可以被拉伸
spacingWidth
1
每一列之间的间距会被拉伸,因此使用该拉伸模式时,必须指定columnWidth,而指定horizontalSpacing就会无效,即不需要先考虑horizontalSpacing
columnWidth
2
每一列是等宽度,只需要指定numColumns和horizontalSpacing,即先指定columnWidth就是无效的
spacingWidthUniform
3
每一列的间距均匀拉伸。拉伸被禁用,不可以被拉伸
3.GridView水平滚动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
private
GridView gridView;
private
float
density;
private
int
listSize =
10
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(
new
MyAdapter());
// 得到像素密度
DisplayMetrics outMetrics =
new
DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
density = outMetrics.density;
// 像素密度
// 根据item的数目,动态设定gridview的宽度,现假定每个item的宽度和高度均为100dp,列间距为5dp
ViewGroup.LayoutParams params = gridView.getLayoutParams();
int
itemWidth = (
int
) (
100
* density);
int
spacingWidth = (
int
) (
5
*density);
params.width = itemWidth*listSize+(listSize-
1
)*spacingWidth;
gridView.setStretchMode(GridView.NO_STRETCH);
// 设置为禁止拉伸模式
gridView.setNumColumns(listSize);
gridView.setHorizontalSpacing(spacingWidth);
gridView.setColumnWidth(itemWidth);
gridView.setLayoutParams(params);
/*
* 使用这种方式也有很大的弊端,由于动态指定了gridview的宽度,导致了gridview的所有item,getView()都会被调用,
* 如果item很大时,就不能使用这种方式了~~~
*/
gridView.setOnItemClickListener(
new
OnItemClickListener() {
@Override
public
void
onItemClick(AdapterView<?> parent, View view,
int
position,
long
id) {
System.out.println(position);
}
});
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
>
<
HorizontalScrollView
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:scrollbars
=
"none"
>
<
LinearLayout
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
>
<
GridView
android:id
=
"@+id/gridView1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
>
</
GridView
>
</
LinearLayout
>
</
HorizontalScrollView
>
</
RelativeLayout
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:background
=
"@android:color/holo_green_light"
android:gravity
=
"center"
android:orientation
=
"vertical"
>
<
ImageView
android:id
=
"@+id/imageView1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:src
=
"@drawable/main_item_ad"
/>
</
LinearLayout
>
|