Android,是否可以在照片库的每个图像中添加文字?

时间:2012-04-25 12:47:28

标签: android view gallery adapter

我正在我的项目中使用照片库,我可以使用我的适配器将图像充气到图库。如果我想在图像底部添加文本(2-3个单词作为图像名称),我该怎么办?是否有参考它?

非常感谢。

3 个答案:

答案 0 :(得分:1)

从适配器的getView方法创建并返回自定义视图对象。您可以在ImageView内垂直方向内展示包含TextViewLinearLayout的XML。

答案 1 :(得分:0)

当您返回图像视图时,您可以使用一些外部Jars来修改图像。试试你的运气。 Read this similar question

答案 2 :(得分:0)

第一: 创建具有textview和imageview的gallery_items布局,

然后你必须使用ViewHolder类

来扩展getView方法中的布局

如下:

 public class GalleryPhotoWithText extends Activity {
  private  Gallery galleryView; 
    /** Called when the activity is first created. */
    @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);

      galleryView = (Gallery)findViewById(R.id.galleryid);

       galleryView.setAdapter(new ImageAdapter(this));         
           }


 public class ImageAdapter extends BaseAdapter { 
  private Activity activity;  
  private  LayoutInflater inflater=null; 
  public ImageAdapter(Activity a) {   
      activity = a;  
           inflater = (LayoutInflater)activity.getSystemService
                   (Context.LAYOUT_INFLATER_SERVICE);          } 

 public int getCount() {   
    return data.length;         }  

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

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

public  class ViewHolder{   
    public TextView text;    
    public ImageView image;         }   

public View getView(int position, View convertView, ViewGroup parent) {   
    View vi=convertView;   
    ViewHolder holder;   
    if(convertView==null){  
        vi = inflater.inflate(R.layout.gallery_items, null); 
        holder=new ViewHolder();  
        holder.text=(TextView)vi.findViewById(R.id.textView1);  
        holder.image=(ImageView)vi.findViewById(R.id.image);   
        vi.setTag(holder);             }  

        else    

    holder=(ViewHolder)vi.getTag();  
    holder.text.setText(name[position]); 
    final int stub_id=data[position];  
    holder.image.setImageResource(stub_id);  
    return vi;         } 

    private int[] data = {  
        R.drawable.image1, R.drawable.image2,  
        R.drawable.image3, R.drawable.image4,  


                        };  
    private String[] name = {  
        "first pic name", "second pic name",  
        "third pic name", "fourth pic name",  



        };     } }

希望这个帮助