GridView显示ICON和TEXT

1.设置GridView的布局

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

2.设置Icon+Text的布局

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_height="wrap_content"   
    android:layout_width="wrap_content"  
    android:orientation="vertical"  
    android:gravity="center"  
    android:layout_gravity="center"  
    >  
    <LinearLayout   
        android:layout_height="wrap_content"   
        android:layout_width="wrap_content"  
        android:orientation="vertical"  
        android:gravity="center"  
        android:layout_gravity="center">  
        <ImageView   
            android:id="@+id/icon_image"  
            android:layout_height="80dip"   
            android:layout_width="80dip"  
            android:gravity="center"  
            android:layout_gravity="center"   
            >  
        </ImageView>  
        <TextView   
            android:id="@+id/icon_text"  
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"  
            android:singleLine="true"  
            android:gravity="center"  
            android:layout_gravity="center"  
            android:textSize="13px"  
            android:layout_marginTop="4dip"  
            >  
        </TextView>  
    </LinearLayout>   
</FrameLayout>

3.创建显示的Adapter

public class HomeImageAdapter extends BaseAdapter {
	
	private Context mContext;
	private LayoutInflater mInflater;
	LinearLayout.LayoutParams params;  
	
	 private Integer[] mThumbIds = {
			 	R.drawable.s81, R.drawable.s82,
			 	R.drawable.s84, R.drawable.s87,
	            R.drawable.s94
	    };
	 
	 private String[] mStrings = {"联系电话","国家","省","城市","城市II"};
	
	public HomeImageAdapter(Context c){
		mContext = c;
		mInflater = LayoutInflater.from(c);
		params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
        params.gravity = Gravity.CENTER; 
	}

	@Override
	public int getCount() {
		return mThumbIds.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ItemViewTag viewTag;  
		
		if (convertView == null)  
        {  
            convertView = mInflater.inflate(R.layout.home_icontext, null);  
              
            // construct an item tag   
            viewTag = new ItemViewTag((ImageView) convertView.findViewById(R.id.icon_image), (TextView) convertView.findViewById(R.id.icon_text));  
            convertView.setTag(viewTag);  
        } else  
        {  
            viewTag = (ItemViewTag) convertView.getTag();  
        }  
          
        // set name   
        viewTag.mName.setText(mStrings[position]);  
          
        // set icon   
        viewTag.mIcon.setBackgroundResource(mThumbIds[position]);  
        viewTag.mIcon.setLayoutParams(params);  
        return convertView;  
        
	}
	
	class ItemViewTag  
    {  
        protected ImageView mIcon;  
        protected TextView mName;  
          
        /** 
         * The constructor to construct a navigation view tag 
         *  
         * @param name 
         *            the name view of the item 
         * @param size 
         *            the size view of the item 
         * @param icon 
         *            the icon view of the item 
         */  
        public ItemViewTag(ImageView icon, TextView name)  
        {  
            this.mName = name;  
            this.mIcon = icon;  
        }  
    }  

}

相关推荐