GridView 元素间距设定

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<GridView 
    android:id="@+id/grid"   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:layout_centerInParent="true" 
     
    android:horizontalSpacing="50dp"  
    android:verticalSpacing="50dp"  
    /> 
</RelativeLayout> 

 

GriUsage.java

public class GridUsage extends Activity { 
    GridView grid; 
    ImageAdapter iAdapter; 
     
    String[] text = { 
            "one","two","three","four","five","six","seven","eight","nine","ten" 
    }; 
     
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        grid = (GridView)findViewById(R.id.grid); 
        iAdapter = new ImageAdapter(this); 
         
        grid.setAdapter(iAdapter); 
        grid.setNumColumns(3); 
         
    } 
     
    public class ImageAdapter extends BaseAdapter { 
        Activity activity; 
         
        public ImageAdapter(Activity a){ 
            activity = a; 
        } 
        @Override 
        public int getCount() { 
            // TODO Auto-generated method stub 
            return text.length; 
        } 
 
        @Override 
        public Object getItem(int arg0) { 
            // TODO Auto-generated method stub 
            return null; 
        } 
 
        @Override 
        public long getItemId(int arg0) { 
            // TODO Auto-generated method stub 
            return arg0; 
        } 
 
        @Override 
        public View getView(int position, View convertView, ViewGroup parent) { 
            // TODO Auto-generated method stub 
            TextView tv; 
             
            if(convertView == null){ 
                tv = new TextView(activity); 
            } 
            else { 
                tv = (TextView)convertView; 
            } 
             
            tv.setSingleLine(true); 
            tv.setBackgroundResource(R.drawable.back); 
            tv.setGravity(Gravity.CENTER); 
            tv.setText(text[position]); 
            return tv; 
        } 
         
    } 
     
}

 

from:http://griffinshi.javaeye.com/blog/663177

你可能感兴趣的:(GridView 元素间距设定)