android网格菜单

时间:2011-04-25 10:15:29

标签: android

大家好我想制作网格菜单,我正在使用这个tutorial

请指导我如何设置不同的图标图片。谢谢

1 个答案:

答案 0 :(得分:6)

制作列表/图像数组(图像ID)&将图像设置在以下行。

        @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);
            TextView tv = (TextView)v.findViewById(R.id.icon_text);
            tv.setText("Profile "+position);
            ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
    --->    iv.setImageResource(R.drawable.icon);

        }
        else
        {
            v = convertView;
        }
        return v;
    }

更改此iv.setImageResource(R.drawable.icon);

iv.setImageResource(images[position]);

这是我的完整代码之一

 import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuItemAdapter extends BaseAdapter
{
    Context context;

    private String[] menuNames = {"Menu 1",
                                  "Menu 2",
                                  "Menu 3",
                                  "Menu 4",
                                  "Menu 5",
                                  "Menu 6"};
    private int[] menuIcons = { R.drawable.img0,
                               R.drawable.img1,
                               R.drawable.img2,
                               R.drawable.img3,
                               R.drawable.img4,
                               R.drawable.img4};


    public MenuItemAdapter(Context context)
    {
        // TODO Auto-generated constructor stub
        this.context = context;
    }

    @Override
    public int getCount()
    {
        // TODO Auto-generated method stub
        return menuNames.length;
    }

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

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

    private class ViewHolder
    {
        public ImageView icon;
        public TextView label;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub
        View vi = convertView;
        ViewHolder holder;

        if(convertView == null)
        {
            vi = LayoutInflater.from(context).inflate(R.layout.menu_item_layout, null);
            holder = new ViewHolder();

            holder.icon = (ImageView) vi.findViewById(R.id.menu_icon);
            holder.label = (TextView) vi.findViewById(R.id.menu_text);

            vi.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) vi.getTag();
        }
        holder.icon.setImageResource(menuIcons[position]);
        holder.label.setText(menuNames[position]);
        return vi;
    }

}

不满:只是我代码的一部分

<GridView
        android:id="@+id/menu_list"
        android:numColumns="3"
        android:gravity="center_horizontal"
        android:horizontalSpacing="5dp"
        android:verticalSpacing="5dp"
        android:stretchMode="columnWidth"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
    />

这是活动

public class YourActivity extends Activity implements OnItemClickListener {

    private GridView menuList;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_layout);

        menuList = (GridView) findViewById(R.id.menu_list);
        menuList.setAdapter(new MenuItemAdapter(this));
        menuList.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id)
    {
        // TODO Auto-generated method stub

    }

}