http://developer.android.com/resources/tutorials/views/hello-gridview.html 在上面的示例代碼中,我想在每個圖像上添加一個文本。請幫助我如何做到這一點?Android:在gridview中,如何在圖像上添加標籤
0
A
回答
0
以下鏈接(的GridView有圖標和文本)可以幫助你
0
1-創建要用於每個項目的GridView裏面的佈局,包含imageview和textview。
2-創建在XML佈局你的GridView。(主要活動)
3-添加ImageAdapter類(或方法)用於綁定數據的照片的主列表。
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.panoramio;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Adapter used to bind data for the main list of photos
*/
public class ImageAdapter extends BaseAdapter {
/**
* Maintains the state of our data
*/
private ImageManager mImageManager;
private Context mContext;
private MyDataSetObserver mObserver;
/**
* Used by the {@link ImageManager} to report changes in the list back to
* this adapter.
*/
private class MyDataSetObserver extends DataSetObserver {
@Override
public void onChanged() {
notifyDataSetChanged();
}
@Override
public void onInvalidated() {
notifyDataSetInvalidated();
}
}
public ImageAdapter(Context c) {
mImageManager = ImageManager.getInstance(c);
mContext = c;
mObserver = new MyDataSetObserver();
mImageManager.addObserver(mObserver);
}
/**
* Returns the number of images to display
*
* @see android.widget.Adapter#getCount()
*/
public int getCount() {
return mImageManager.size();
}
/**
* Returns the image at a specified position
*
* @see android.widget.Adapter#getItem(int)
*/
public Object getItem(int position) {
return mImageManager.get(position);
}
/**
* Returns the id of an image at a specified position
*
* @see android.widget.Adapter#getItemId(int)
*/
public long getItemId(int position) {
PanoramioItem s = mImageManager.get(position);
return s.getId();
}
/**
* Returns a view to display the image at a specified position
*
* @param position The position to display
* @param convertView An existing view that we can reuse. May be null.
* @param parent The parent view that will eventually hold the view we return.
* @return A view to display the image at a specified position
*/
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
// Make up a new view
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.image_item, null);
} else {
// Use convertView if it is available
view = convertView;
}
PanoramioItem s = mImageManager.get(position);
ImageView i = (ImageView) view.findViewById(R.id.image);
i.setImageBitmap(s.getBitmap());
i.setBackgroundResource(R.drawable.picture_frame);
TextView t = (TextView) view.findViewById(R.id.title);
t.setText(s.getTitle());
t = (TextView) view.findViewById(R.id.owner);
t.setText(s.getOwner());
return view;
}
}
4-填寫你的GridView與從BaseAdapter
MyGridView = (GridView)findViewById(R.id.MyGridView);
MyGridView .setAdapter(new ImageAdapter(this));
對不起延長我的英語不好的ImageAdapter類,希望它可以幫助你。
+0
Anass,Sorry..where你在上面的代碼中設置了圖片上的文字(標籤)嗎? – user893195
相關問題
- 1. 如何在gridview上添加圖像?
- 2. 如何在圖片上添加標籤?
- 3. 如何在android中的gridview中添加所選圖像
- 4. 如何在TensorBoard圖像上添加標籤?
- 5. 如何根據標籤值屬性在uiimageview上添加圖像。
- 6. 如何在android listview或gridview中動態添加圖像/圖像按鈕
- 7. 如何在WinForms中將圖像添加到TabControl的標籤中?
- 8. android如何在圖像上添加標記?
- 9. 如何在張量流中給圖像添加多個標籤?
- 10. 如何在SWT標籤中添加文本和圖像
- 11. 在GridView中加載異步圖像(Android)
- 12. 在按鈕上添加圖像圖標?
- 13. 在條形圖上添加標籤Raphael
- 14. 如何在Android應用程序欄中添加徽標圖像?
- 15. 在標籤中添加上標 - Flex
- 16. 如何添加圖像圖標在android的listview?
- 17. 如何在圖像製作後添加碼頭標籤
- 18. 如何在flex中添加下標/上標到按鈕標籤?
- 19. 如何在gridview分頁中添加圖像
- 20. 如何在android中動態地在主按鈕上添加圖像按鈕和標籤?
- 21. 在活動的可切換標籤上添加圖像HTML/CSS
- 22. 如何在左列的gridview邊添加標籤?
- 23. 如何在android中的操作欄上添加多個圖標?
- 24. 如何在某些按鈕上添加圖像圖標?
- 25. 在標籤中拉伸的圖像android
- 26. 在android中的另一個圖像上添加圖像
- 27. 如何在EditText上添加圖像
- 28. 如何在圖像上添加水印?
- 29. 如何在圖像上添加筆劃?
- 30. 在地圖上添加標記Android
鏈接不再工作。 – WeirdElfB0y