我需要一個GridView,但是在每個網格中,其上會有一個ImageView和TextView。如何將TextView添加到GridView佈局中的ImageView中?
它將像每個網格中的項目圖像和圖像上的項目名稱。
我特林:
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,我無法實現。
UPDATE:
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;
}
這解決了我的問題
膨脹的佈局後,我應該怎麼回報? – Burak
Relativelayouts將是您的GridView的項目。因此請返回相關佈局參考。 – Ronnie