我如何添加一个TextView到GridView控件布局的ImageView的? [英] How can I add a TextView into an ImageView in GridView Layout?

查看:181
本文介绍了我如何添加一个TextView到GridView控件布局的ImageView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个GridView,但在每一个网格,将有一个的ImageView和TextView的过/里面

I need a GridView, but in each grid, there will be an ImageView and TextView over/inside it.

它会像在每个网格的图像中的项目的图像,并且该项目的名称

It will be like an item image in each grid, and name of the item on the image.

我特林:

    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(300, 300));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setPadding(0, 0, 0, 0);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);

    TextView nameView = new TextView(mContext);
    nameView.setText("Name");
    nameView.setTextSize(20);

    parent.addView(nameView);

    return imageView;
}

在我的网格视图适配器。但我不能在这里添加它。此外,我试着将它添加在ImageView的,但因为它不是一个ViewGroup中,我无法实现的。

in my grid view adapter. But I cannot add it here. Also I tried to add it in ImageView but since it is not a ViewGroup, I couldn't achieve.

更新:

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

    LayoutInflater inflater = LayoutInflater.from(mContext);
    ViewHolder holder;

    if (convertView == null) { // if it's not recycled, initialize some
                                // attributes
        convertView = inflater.inflate(R.layout.gridviewitem, null, true);
        holder = new ViewHolder();
        holder.image = (ImageView) convertView
                .findViewById(R.id.gridViewItemImage);
        holder.name = (TextView) convertView
                .findViewById(R.id.gridViewItemName);
        holder.price = (TextView) convertView
                .findViewById(R.id.gridViewItemPrice);
        convertView.setTag(holder);

    } else {    
        holder = (ViewHolder) convertView.getTag();
    }

    holder.image.setImageResource(mThumbIds[position]);
    holder.name.setText("Item " + String.valueOf(position));
    holder.price.setText("$15");

    return convertView;
}

这解决了我的问题。

推荐答案

创建一个 RelativeLayout的包含的ImageView 和TextView中,用的TextView的底部边缘对齐的的ImageView 底边。

Create a RelativeLayout that contains the ImageView and the TextView,with TextView's bottom edge aligned with the Imageview's bottom edge.

<RelativeLayout ...>
    <ImageView ...>
    <TextView ...
      android:layout_alignBottom="id of the imageview"> // or to top
</RelativeLayout>

这个充气布局 getView 适配器的方法。

这篇关于我如何添加一个TextView到GridView控件布局的ImageView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆