GridView 弄的 StringGrid

1、

主界面 layout_main.xml :

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="z.stringgridandroidz.MainActivity" >
10 
11     <HorizontalScrollView
12         android:id="@+id/horizontalScrollView1"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:layout_alignParentBottom="true"
16         android:layout_alignParentLeft="true"
17         android:layout_alignParentRight="true"
18         android:layout_alignParentTop="true" >
19 
20         <LinearLayout
21             android:layout_width="match_parent"
22             android:layout_height="match_parent"
23             android:orientation="vertical" >
24 
25             <GridView android:id="@+id/gridView1"
26                 android:layout_height="wrap_content" 
27                 android:layout_width="match_parent" 
28                 android:numColumns="5">
29             </GridView>
30 
31         </LinearLayout>
32     </HorizontalScrollView>
33 </RelativeLayout>

 

ZC:<LinearLayout/> 和 <HorizontalScrollView/> 是一起的,界面上也就一个 <GridView/>...

 

2、

在 res-->layout 中添加一个 items.xml 用于每个GridView单元格的样式,内容为:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout android:id="@+id/LinearLayout01"
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:layout_width="fill_parent" android:background="#555555"
 5     android:layout_height="wrap_content">
 6     <TextView android:layout_below="@+id/ItemImage" android:text="TextView01"
 7         android:id="@+id/ItemText" android:bufferType="normal"
 8         android:singleLine="true" android:background="#FFFFFF"
 9         android:layout_width="fill_parent" android:gravity="center"
10         android:layout_margin="1dip" android:layout_gravity="center"
11         android:layout_height="wrap_content">
12     </TextView>
13 </LinearLayout>

 

3、

测试代码:

 1 package z.stringgridandroidz;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 
 6 import android.app.Activity;
 7 import android.os.Bundle;
 8 import android.widget.GridView;
 9 import android.widget.LinearLayout;
10 import android.widget.SimpleAdapter;
11 
12 public class MainActivity extends Activity {
13 
14     @Override
15     protected void onCreate(Bundle savedInstanceState)
16     {
17         super.onCreate(savedInstanceState);
18         setContentView(R.layout.activity_main);
19         
20     // ***
21         
22         // 数据源
23         ArrayList<HashMap<String, String>> dateSource = new ArrayList<HashMap<String, String>>();
24         // 向数据源中添加数据
25         for (int i = 0; i < 1600; i++)
26         {
27             HashMap<String, String> map = new HashMap<String, String>();
28             map.put("ItemText", Integer.toString(i)+"_123456789");
29             dateSource.add(map);
30         }
31         
32         // Adapter
33         SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,
34                 dateSource,// 数据来源
35                 R.layout.items,//XML实现
36                 new String[] { "ItemText" },// 动态数组与ImageItem对应的子项
37                 new int[] { R.id.ItemText });
38  
39         GridView gv = (GridView) this.findViewById(R.id.gridView1);  
40         gv.setAdapter(simpleAdapter);
41         
42         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(  
43                 800, LinearLayout.LayoutParams.WRAP_CONTENT);  
44         gv.setLayoutParams(params);
45         
46         simpleAdapter.notifyDataSetChanged();
47     }
48 
49 }

 

4、

缺点:

4.1、

GridView 的宽度,需要自己在程序里面设置:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( 800, LinearLayout.LayoutParams.WRAP_CONTENT );
gv.setLayoutParams(params);

 

4.2、

GridView 默认的 垂直的滚动条是在最右边的...

需要将 GridView 翻到最右边才能看到垂直的滚动条...

 

 

 

 

 

X

 

posted @ 2015-11-11 14:19  codeskill_android  阅读(210)  评论(0编辑  收藏  举报