2011-12-09 85 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" 
};