在网格视图中添加标签

发布于 2024-12-10 05:14:44 字数 2107 浏览 0 评论 0原文

我想在 Android 中的网格视图中为图标添加标签。我找到了一些答案,但没有一个对我有用。大多数时候应用程序会被强制关闭。 任何帮助将不胜感激。

这是我正在使用的代码

package com.appsdrip16;

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

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

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

    public Object getItem(int position) {
        return null;
    }

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(1, 1, 1, 1);

            Bundle bundle=new Bundle();
            bundle.putInt("key", position); 

          //Inflate the layout
            LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View MyView = li.inflate(R.layout.main, null);

            //View MyView;
            // Add The Text!!!
            TextView tv = (TextView) MyView.findViewById(R.id.grid_item_text);
            tv.setText("Item "+ position );

        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }
    // references to our images
    private Integer[] mThumbIds = { 
              R.drawable.icon1, R.drawable.icon2,
              R.drawable.icon3, R.drawable.icon4
             };
}

I want to add labels for the icons in grid view in Android.I found some answers for this but none worked for me.Most of the time the application get closed forcefully.
Any help will be appreciated.

here's the code that i am using

package com.appsdrip16;

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

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

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

    public Object getItem(int position) {
        return null;
    }

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(1, 1, 1, 1);

            Bundle bundle=new Bundle();
            bundle.putInt("key", position); 

          //Inflate the layout
            LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View MyView = li.inflate(R.layout.main, null);

            //View MyView;
            // Add The Text!!!
            TextView tv = (TextView) MyView.findViewById(R.id.grid_item_text);
            tv.setText("Item "+ position );

        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }
    // references to our images
    private Integer[] mThumbIds = { 
              R.drawable.icon1, R.drawable.icon2,
              R.drawable.icon3, R.drawable.icon4
             };
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜宝宝 2024-12-17 05:14:44

为了实现这样的自定义 gridview,您必须定义一个自定义行 XML 布局文件,然后在自定义适配器内将其膨胀,以用于带有图标和文本的 GridView。

For implementing such Custom gridview, you have to define a custom row XML layout file and then inflate it inside the custom adapter to be used for your GridView with icons and text.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文