<?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:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:background="@drawable/background"
     >


   </GridView>


帮助添加文本视图

最佳答案

package com.cmc;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

    public class MainSwitch extends Activity{
        GridView grid_main;
        Context context;
        TextView tv;
        ImageAdapter grid;
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            context = this;
            setContentView(R.layout.main);

            grid_main = (GridView)findViewById(R.id.GridView01);
            grid_main.setAdapter(new ImageAdapter(this));
            grid= new ImageAdapter(context);

            grid_main.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView parent, View v, int position, long id) {
                    grid .getCount();
                    grid .getItem(position);
                     Log.v(getClass().getSimpleName(),"Grid222 count.........."+grid.getCount());
                     Log.v(getClass().getSimpleName(),"Gridgetitem .........."+grid.getItem(position));
                     Log.v(getClass().getSimpleName(),"Grid getitemid.........."+grid.getItemId(position));
                      Toast.makeText(context,""+grid_main.getCount() , 4000).show();
                      tv.getText();



                }
            });


        }
        public class ImageAdapter extends BaseAdapter{
            Context mContext;
            public static final int ACTIVITY_CREATE = 10;
            public ImageAdapter(Context c){
                mContext = c;
            }
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return 5;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                View v;
                if(convertView==null){
                    LayoutInflater li = getLayoutInflater();
                    v = li.inflate(R.layout.icon, null);
                    tv = (TextView)v.findViewById(R.id.icon_text);
                    tv.setText("Icon.. "+position);
                    ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
                    iv.setImageResource(R.drawable.icon);

                }
                else
                {
                    v = convertView;
                }
                return v;
            }
            public Object getItem(int position) {
                return position;
            }

            public long getItemId(int position) {
                return position;
            }
        }
    }

08-05 13:33