2011-12-09 89 views
2

有没有办法将文本标签添加到android gridview?我使用的是标准的Android示例,Hello GridView添加标签/文本到Android Gridview

例如,如果我想在每张图片下面添加图片标题,有没有办法做到这一点?如果是这样,有人可以解释这将如何工作?或者,有没有其他的方法来实现这一点?谢谢!

回答

7

您可以创建自定义的GridView像ListView中我们自定义的布局,以显示ListRow你还可以创建自定义布局为GridView项目 见this Example对于GridView控件。

使用简单的GridView您现在可以只显示一个ImageView, 现在如果要为每个Grid Item添加另一个视图(文本),则需要创建一个XML文件以包含TextView和ImageView。

然后在适配器和GetView方法中使用该XML文件,您可以将图像和文本设置为您的视图。

这里是another example为相同的,这里是另一个Good Example

+1

太好了!这完全是我在找的东西! – user836200

+0

您是否在最近一次编辑后检查了答案,我添加了2个以上的链接,我建议请他们对Custom GridView有更多的了解。 – MKJParekh

+0

这些都是很好的例子(都比第一个更好)。再次感谢! – user836200

1

为Exaample采取LinearLayout并添加一个imageview,textview和膨胀该布局和assingn convertview。

0

调用此适配器从活动

 GridView view =(GridView) findViewbyId(is of grid view here); 
     view.setAdapter(new BottomMenuGridAdaptor(this.context)); 

这是代码适配器

 public class BottomMenuGridAdaptor extends BaseAdapter { 


private Context context; 
public BottomMenuGridAdaptor(Context context) { 
    super(); 
    this.context = context; 
} 

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

@Override 
public Object getItem(int position) { 
    return position; 
} 

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

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

    if (convertView == null){ 
     convertView = LayoutInflater.from(context).inflate(R.layout.tab_button, null); 
    } 
    LinearLayout ll=(LinearLayout) convertView.findViewById(R.id.linear_topbar); 

    if(position==PostionHandler.getPosition(context)) 
     ll.setBackgroundResource(R.drawable.tab_blue_bg); 
    else 
     ll.setBackgroundResource(R.drawable.tab_red_bg); 

    ImageView imageView=(ImageView)convertView.findViewById(R.id.icon); 
    imageView.setImageResource(mThumbIds[position]); 
    TextView txt= (TextView) convertView.findViewById(R.id.title); 
    txt.setText(mThumbtext[position]); 
    return convertView; 
} 

private Integer[] mThumbIds = { 
     R.drawable.temp_thisweek, 
     R.drawable.temp_store, 
     R.drawable.temp_mylibrary, 
     R.drawable.temp_lateststore, 
     R.drawable.temp_more 
}; 

private String[] mThumbtext = { 
     "This Week", 
     "C&EN Store", 
     "My Library", 
     "Latest News", 
     "More" 
};