gridview--基本的gridview

GridView 元素距离设定

因为该设定比较简单 防止以后忘记 所以贴 供自己查阅

1. 布局:main.xml

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <GridView  
  8.     android:id="@+id/grid"    
  9.     android:layout_width="fill_parent"   
  10.     android:layout_height="wrap_content"   
  11.     android:layout_centerInParent="true"  
  12.       
  13.     android:horizontalSpacing="50dp"   
  14.     android:verticalSpacing="50dp"   
  15.     />  
  16. </RelativeLayout>  

2. *.jave: GriUsage.java

Java代码  收藏代码
  1. public class GridUsage extends Activity {  
  2.     GridView grid;  
  3.     ImageAdapter iAdapter;  
  4.       
  5.     String[] text = {  
  6.             "one","two","three","four","five","six","seven","eight","nine","ten"  
  7.     };  
  8.       
  9.     /** Called when the activity is first created. */  
  10.     @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.main);  
  14.           
  15.         grid = (GridView)findViewById(R.id.grid);  
  16.         iAdapter = new ImageAdapter(this);  
  17.           
  18.         grid.setAdapter(iAdapter);  
  19.         grid.setNumColumns(3);  
  20.           
  21.     }  
  22.       
  23.     public class ImageAdapter extends BaseAdapter {  
  24.         Activity activity;  
  25.           
  26.         public ImageAdapter(Activity a){  
  27.             activity = a;  
  28.         }  
  29.         @Override  
  30.         public int getCount() {  
  31.             // TODO Auto-generated method stub  
  32.             return text.length;  
  33.         }  
  34.   
  35.         @Override  
  36.         public Object getItem(int arg0) {  
  37.             // TODO Auto-generated method stub  
  38.             return null;  
  39.         }  
  40.   
  41.         @Override  
  42.         public long getItemId(int arg0) {  
  43.             // TODO Auto-generated method stub  
  44.             return arg0;  
  45.         }  
  46.   
  47.         @Override  
  48.         public View getView(int position, View convertView, ViewGroup parent) {  
  49.             // TODO Auto-generated method stub  
  50.             TextView tv;  
  51.               
  52.             if(convertView == null){  
  53.                 tv = new TextView(activity);  
  54.             }  
  55.             else {  
  56.                 tv = (TextView)convertView;  
  57.             }  
  58.               
  59.             tv.setSingleLine(true);  
  60.             tv.setBackgroundResource(R.drawable.back);  
  61.             tv.setGravity(Gravity.CENTER);  
  62.             tv.setText(text[position]);  
  63.             return tv;  
  64.         }  
  65.           
  66.     }  
  67.       
  68. }  

3. emulator 运行截图:

that's all.  any idea or other are welcome!!!!

原文地址:https://www.cnblogs.com/awkflf11/p/5061904.html