GridView网格布局,默认情况下是没有网格线的
查找网上资料,找到了一种为GridView添加网格线的小技巧
实际上,该网格线是通过设置GridView各子项的间隔,并分别设置GridView背景色与子项背景色实现的。
实现方法
1. 设置GridView背景色,设置水平间方向间隔属性值android:horizontalSpacing和竖直方向间隔属性值android:verticalSpacing
2. 设置GridView子项背景色
示例代码:
main.xml
1. <?xmlversion="1.0"encoding="utf-8"?>
2. <GridViewxmlns:android="http://schemas.android.com/apk/res/android"
3. android:id="@+id/main_GridView"
4. android:horizontalSpacing="1dp"
5. android:verticalSpacing="1dp"
6. android:stretchMode="columnWidth"
7. android:numColumns="3"
8. android:gravity="center"
9. android:listSelector="@null"
10. android:background="#DCDCDC"
11. android:layout_width="fill_parent"
12. android:layout_height="fill_parent">
13.
14. </GridView>
[xml] 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/main_GridView"
4. android:horizontalSpacing="1dp"
5. android:verticalSpacing="1dp"
6. android:stretchMode="columnWidth"
7. android:numColumns="3"
8. android:gravity="center"
9. android:listSelector="@null"
10. android:background="#DCDCDC"
11. android:layout_width="fill_parent"
12. android:layout_height="fill_parent">
13.
14. </GridView>
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_GridView"
android:horizontalSpacing="1dp"
android:verticalSpacing="1dp"
android:stretchMode="columnWidth"
android:numColumns="3"
android:gravity="center"
android:listSelector="@null"
android:background="#DCDCDC"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</GridView>
Main.java
1. package dyingbleed.iteye;
2.
3. import android.app.Activity;
4. import android.content.Context;
5. import android.graphics.Color;
6. import android.os.Bundle;
7. import android.view.Gravity;
8. import android.view.View;
9. import android.view.ViewGroup;
10. import android.view.ViewGroup.LayoutParams;
11. import android.widget.AbsListView;
12. import android.widget.BaseAdapter;
13. import android.widget.GridView;
14. import android.widget.TextView;
15.
16. publicclass Main extends Activity {
17.
18. private GridView grid;
19.
20. @Override
21. publicvoid onCreate(Bundle savedInstanceState) {
22. super.onCreate(savedInstanceState);
23. setContentView(R.layout.main);
24.
25. grid = (GridView) findViewById(R.id.main_GridView);
26. grid.setAdapter(new GridViewAdapter(this));
27. }
28.
29. privateclass GridViewAdapter extends BaseAdapter {
30.
31. private Context context;
32.
33. public GridViewAdapter(Context context) {
34. this.context = context;
35. }
36.
37. int count = 100;
38.
39. @Override
40. publicint getCount() {
41. return count;
42. }
43.
44. @Override
45. public Object getItem(int position) {
46. return position;
47. }
48.
49. @Override
50. publiclong getItemId(int position) {
51. return position;
52. }
53.
54. @Override
55. public View getView(int position, View convertView, ViewGroup parent) {
56. TextView result = new TextView(context);
57. result.setText("Item "+position);
58. result.setTextColor(Color.BLACK);
59. result.setTextSize(24);
60. result.setLayoutParams(new AbsListView.LayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)));
61. result.setGravity(Gravity.CENTER);
62. result.setBackgroundColor(Color.WHITE); //设置背景颜色
63. return result;
64. }
65.
66. }
67. }
[java] view plaincopyprint?
1. package dyingbleed.iteye;
2.
3. import android.app.Activity;
4. import android.content.Context;
5. import android.graphics.Color;
6. import android.os.Bundle;
7. import android.view.Gravity;
8. import android.view.View;
9. import android.view.ViewGroup;
10. import android.view.ViewGroup.LayoutParams;
11. import android.widget.AbsListView;
12. import android.widget.BaseAdapter;
13. import android.widget.GridView;
14. import android.widget.TextView;
15.
16. public class Main extends Activity {
17.
18. private GridView grid;
19.
20. @Override
21. public void onCreate(Bundle savedInstanceState) {
22. super.onCreate(savedInstanceState);
23. setContentView(R.layout.main);
24.
25. grid = (GridView) findViewById(R.id.main_GridView);
26. grid.setAdapter(new GridViewAdapter(this));
27. }
28.
29. private class GridViewAdapter extends BaseAdapter {
30.
31. private Context context;
32.
33. public GridViewAdapter(Context context) {
34. this.context = context;
35. }
36.
37. int count = 100;
38.
39. @Override
40. public int getCount() {
41. return count;
42. }
43.
44. @Override
45. public Object getItem(int position) {
46. return position;
47. }
48.
49. @Override
50. public long getItemId(int position) {
51. return position;
52. }
53.
54. @Override
55. public View getView(int position, View convertView, ViewGroup parent) {
56. TextView result = new TextView(context);
57. result.setText("Item "+position);
58. result.setTextColor(Color.BLACK);
59. result.setTextSize(24);
60. result.setLayoutParams(new AbsListView.LayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)));
61. result.setGravity(Gravity.CENTER);
62. result.setBackgroundColor(Color.WHITE); //设置背景颜色
63. return result;
64. }
65.
66. }
67. }