如何在gridView中将imageView转换为imageView?

时间:2014-11-03 07:33:04

标签: android android-layout relativelayout

我按照android:gridview上的教程编写了一个应用程序,我想在gridview的每个网格中的图像上添加文本。我已经制作了一个xml文件,我将文本放在imageview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="17dp"
    android:src="@drawable/n" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView1"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_marginBottom="19dp"
    android:layout_marginLeft="30dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#ffffff"
    android:textStyle="bold" />

</RelativeLayout>

和扩展BaseAdapter类的类的代码是

public class TourismImageAdapter extends BaseAdapter    {



private Integer[] mThumbIds = {
        R.drawable.a,
        R.drawable.b,
        R.drawable.c,
        R.drawable.d,
        R.drawable.e,
        };




private Context mContext;
public TourismImageAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView imageView;

    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(340, 300));
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setPadding(2, 2, 2, 2);
    } else {
        imageView = (ImageView) convertView;

    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

}

我应该如何链接xml和java文件,以便在GridView的每个网格上实现图像上的文本。提前致谢

3 个答案:

答案 0 :(得分:0)

      public class TourismImageAdapter extends BaseAdapter   
       {
          private Integer[] mThumbIds = 
          {
           R.drawable.a,
           R.drawable.b,
           R.drawable.c,
           R.drawable.d,
           R.drawable.e,
         };

       private Context mContext;
       public TourismImageAdapter(Context c)
       {
        mContext = c;
       }

       @Override
       public int getCount()
       {
         return mThumbIds.length;
       }

       @Override
       public Object getItem(int position) 
       {
        return mThumbIds[position];
       }

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView imageView;

    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(340, 300));
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setPadding(2, 2, 2, 2);
    } else {
        imageView = (ImageView) convertView;

    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
 }

}

答案 1 :(得分:0)

你的xml应该是这样的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/black"
    android:textStyle="bold" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="17dp"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

这样你可以将xml与你的适配器类链接起来。下面是你的适配器的getView()的代码

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = layoutInflater.inflate(R.layout.partnerslistcontents,
                null);
        holder.textView= (TextView) convertView.findViewById(R.id.textView1);
        holder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }


    holder.textView.setText("");// set your text here
    holder.imageView.setImageResource(mThumbIds[position]);//set your image here

    return convertView;
}

这是您的ViewHolder类

static class ViewHolder {
    ImageView imageView;
    TextView textView;
}

同时添加此

private Context mContext;
private LayoutInflater layoutInflater;
public TourismImageAdapter(Context c){
mContext = c;
layoutInflater = LayoutInflater.from(mContext);
}

答案 2 :(得分:0)

public class TourismImageAdapter extends BaseAdapter {
private Integer[] mThumbIds = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, };
private LayoutInflater mInflater;
private ViewHolder mViewHolder; 

class ViewHolder {
    public TextView textView;
    public ImageView imageView;
}

public TourismImageAdapter(Context c) {
    mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        mViewHolder = new ViewHolder();
        convertView = mInflater.inflate(R.layout.your_xml, parent, false);
        mViewHolder.textView = (TextView) convertView.findViewById(R.id.textView1);
        mViewHolder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);  
        convertView.setTag(mViewHolder);  
    } else {
        mViewHolder = (ViewHolder) convertView.getTag();
    }

    mViewHolder.imageView.setImageResource(mThumbIds[position]);
    mViewHolder.textView.setText("text");

    return convertView;
}

}