如何在Android中将文本和图像(图像URL)设置为Gridview

时间:2012-02-15 10:24:26

标签: android

如何在Android中将GridText和图像(图像URL)设置为Gridview,并且ImageSize应固定为所有图像 注意:我从webservice

收集这些数据(IMage URL和Text)

2 个答案:

答案 0 :(得分:3)

你必须制作自己的布局,例如,

 <RelativeLayout 
        android:id="@+id/relGrid"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"  >

            <GridView  
                android:id="@+id/gridview"
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:columnWidth="90dp"
                android:numColumns="3"
                android:verticalSpacing="10dp"
                android:horizontalSpacing="10dp"
                android:stretchMode="columnWidth"
                android:listSelector="@null" />

    </RelativeLayout>

为网格中的每个项目再制作一个布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:layout_width="wrap_content"
  android:layout_height="120dp"
  android:background="@drawable/background">

    <ImageView 
        android:layout_width="wrap_content"
        android:id="@+id/imgGrid"    
        android:layout_centerHorizontal="true"

        android:layout_height="wrap_content"/>

    <TextView
        android:layout_below="@+id/imgGrid" 
        android:layout_centerInParent="true"
        android:id="@+id/txtTitle"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:gravity="center"
        android:text=""
        android:textStyle="bold"
        android:textSize="15dp"     
        android:textColor="@color/blue"/>


</RelativeLayout>

然后像这样使用gridView,

setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
            gridview.setAdapter(new ImageAdapter(MainActivity.this,R.layout.grid_layout , imgArray, titleArray));

            gridview.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
                {
                    if(position == 0)
                    {
                       //your code
                    }
                }
            });

ImageAdapter.java

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class ImageAdapter extends ArrayAdapter<Object>
{
    String TAG = "ImageAdapter";

    private int resource;
    private Integer[] mImgGrid;
    private String[] mTitleText;

    public ImageAdapter(Context context, int resorce, Integer[] imgGrid,String[] titleText) 
    {
        super(context, resorce, imgGrid);
        Log.i(TAG," in set adapter lstRecipes "+ imgGrid.length );
        this.resource = resorce;
        this.mImgGrid = imgGrid;
        this.mTitleText = titleText;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewHolder holder;

        if (convertView == null)
        {
            LayoutInflater layoutInflater = LayoutInflater.from(getContext());
            convertView  = layoutInflater.inflate(resource, parent, false);
            holder = new ViewHolder();
            holder.txtTitle = (TextView)convertView.findViewById(R.id.txtTitle);
            holder.imgGrid = (ImageView)convertView.findViewById(R.id.imgGrid);

            convertView.setTag(holder);
        }
        else 
        {
             holder=(ViewHolder)convertView.getTag();
        }

        try
        {
            holder.txtTitle.setText(mTitleText[position]);
            holder.imgGrid.setImageResource(mImgGrid[position]);
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }

        return convertView;
    }

    public static class ViewHolder
    {
        private TextView txtTitle;
        private ImageView imgGrid;
    }
}

如果您直接从webservice使用图像和文本,那么您可以从webService解析数据并且可以在此ImageView和TextView中设置它。

答案 1 :(得分:0)

1)为gridview创建一个xml布局。

<?xml version="1.0" encoding="utf-8"?>
<GridView
 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
    android:padding="10dp"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:columnWidth="80dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:background="@color/white"
    >
</GridView>

2)再创建一个布局home_screen_text.xml

 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/widget44"
 android:layout_width="wrap_content"
  android:layout_height="wrap_content"
 android:orientation="vertical"
 android:layout_x="201px"
android:layout_y="165px"
 android:gravity="center_horizontal"
 >
 <ImageView
 android:id="@+id/icon_image"
 android:layout_width="wrap_content"
 android:layout_height="80dp"

  >
 </ImageView>
 <TextView
 android:id="@+id/icon_text"
 android:typeface="serif"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:gravity="center_horizontal"
 android:textColor="@color/black"
 android:textStyle="bold"
 android:lines="2">
</TextView>
</LinearLayout>

3)然后使用gridView。            GridView gridview =(GridView)findViewById(R.id.GridView01);         gridview.setAdapter(new ImageAdapter(this));         gridview.setOnItemClickListener(new OnItemClickListener(){             public void onItemClick(AdapterView parent,View v,                     int position,long id){                                         }                        }

4)创建一个继承baseadapter的imageadapter类。

  public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    public static final int ACTIVITY_CREATE = 10;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds1.size();
    }

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

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

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        View v;

        if (convertView == null) {
            imageView = new ImageView(mContext);
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.home_screen_text, null);
            TextView tv = (TextView) v.findViewById(R.id.icon_text);
            tv.setText("" + iconText.get(position).toString());
            ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
            iv.setImageResource(mThumbIds1.get(position));

        } else {
            v = convertView;
            imageView = new ImageView(mContext);
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.home_screen_text, null);
            TextView tv = (TextView) v.findViewById(R.id.icon_text);
            tv.setText("" + iconText.get(position).toString());
            ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
    iv.setImageDrawble(drawable_from_url("url of the image", "name"));

        }
        return v;
    }
}

返回drawable的方法。    Drawable drawable_from_url(String url,String src_name)抛出java.net.MalformedURLException,java.io.IOException     {        返回Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).awagetContent()),src_name);     }