在gridview中,我想为该图像设置图像和名称?

时间:2011-03-16 06:12:18

标签: android

我成功地从适配器设置了图像。我想在图像下方设置名称。这该怎么做。名称存储在字符串数组中。适配器的代码片段如下所示。

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

        } else {
            imageView = (ImageView) convertView;
        }

我也希望图像和图像标题与下面链接中的图像一样接近。

How to implement custom device photos gallery for android?

谢谢。

1 个答案:

答案 0 :(得分:1)

最好你可以在线性布局中创建一个ImageView和TextView的新布局。(TextView保存在ImageView下)。

public View getView(int position, View convertView, ViewGroup parent) {

            View view = getLayoutInflater().inflate(R.layout.gridview_layout,
                    null);//gridview_layout is the layout tht you have created.
            TextView textView = (TextView) view.findViewById(R.id.Sample);//Sample is the textView id
            ImageView imageView = (ImageView) view.findViewById(R.id.sampleim);//sampleim is the imageview id
            textView.setText(names[position]);//names:your string array of names
            imageView.setImageResource(imageids[position]);//imageids: String array of images.
            view.setLayoutParams(new GridView.LayoutParams(200, 200));
            return view;
        }