在android中添加对自定义库视图的描述

时间:2011-09-05 07:12:48

标签: android custom-controls gallery

有人知道如何添加图片名称或图片描述,以便在每张图片的投影时也能更改。

我能够通过获取单独的文本视图并在适配器的getView()中膨胀此文本视图来实现。但它效率不高。 所以请帮我找另一种方式...

我的代码:

**public class ImageAdapter extends BaseAdapter {
    int mGalleryItemBackground;
    private Context mContext;
    private Vector<ListObjectWithTwoTextAndOneImage> data;
    public ImageAdapter(Context c,
            Vector<ListObjectWithTwoTextAndOneImage> d)
{
    super();
        mContext = c;
        data = d;

    }
    public int getCount() {
        return data.size();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        **fishName.setText(data.get(position).getFirstItem());**
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(mContext);

        imageView.setImageBitmap((Utils.getInstance().getImageFromAssets(
                PhotoGalleryGallery.this, data.get(position)
                        .getThumbNailImageUrl() + ".jpg"))); // TODO: change
        imageView.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        // imageView.setBackgroundResource(mGalleryItemBackground);
        return imageView;
    }
}**

提前致谢

2 个答案:

答案 0 :(得分:2)

谢谢......我还得到了另一种替代解决方案.. 我刚刚在get中创建了一个Linear布局,然后添加了一个图像和文本视图,....

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

        LinearLayout layout = new LinearLayout(getApplicationContext());
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(new Gallery.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


        ImageView imageView = new ImageView(mContext);
        imageView.setImageBitmap((Utils.getInstance().getImageFromAssets(
                PhotoGalleryGallery.this, data.get(position)
                        .getThumbNailImageUrl() + ".jpg"))); // TODO: change

        if (Configuration.ORIENTATION_PORTRAIT==PhotoGalleryGallery.this.getResources().getConfiguration().orientation) {

            imageView.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);

            TextView tv = new TextView(mContext);
            tv.setText(data.get(position).getFirstItem());
            tv.setTextColor(Color.WHITE);
            tv.setTypeface(null, Typeface.BOLD);
            tv.setPadding(20, 20, 0, 5);
            tv.setMaxLines(2);
            tv.setMinLines(2);

            ImageView divider= new ImageView(mContext);
            divider.setBackgroundResource(R.drawable.devider);
            divider.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            divider.setScaleType(ImageView.ScaleType.FIT_XY);

            TextView tv1 = new TextView(mContext);
            tv1.setText(data.get(position).getSecondItem());
            tv1.setTextColor(Color.WHITE);
            tv1.setTypeface(null, Typeface.BOLD);
            tv1.setPadding(20, 5, 0, 0);
            tv1.setMinLines(2);
            tv1.setMaxLines(2);

            layout.addView(imageView);
            layout.addView(tv);
            layout.addView(divider);
            layout.addView(tv1);

        } else {
            imageView.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            layout.addView(imageView);

        }
        picPosition=position;

        return layout;
    }

答案 1 :(得分:0)

请浏览以下链接。我认为这将解决您的问题:Android gallery with caption