Android网格视图与图像和文本

时间:2016-02-14 09:07:32

标签: java android android-gridview

我需要在网格视图中的每个图像下方显示文字。我已经能够将图像放在网格视图中,但如何将文本放在它下面?我在下面发布了代码段。

fragment_facility_grid.xml

package com.androidbelieve.HIT_APP;

/**
 * Created by Akash on 2/13/2016.
 */
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class FacilityAdapter extends BaseAdapter {
    private Context mContext;


    // Keep all Images in array
    public Integer[] mThumbIds = {
            R.drawable.bank, R.drawable.bus,
            R.drawable.girlshostel , R.drawable.guesthouse,
            R.drawable.gym , R.drawable.library,R.drawable.sports

    };

    public String[] mThumbNames = {
            "Bank", "Bus Service","Guest House", "GYM","Fac1","Fac2"

    };

    // Constructor
    public FacilityAdapter(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 0;
    }

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

        View gridView;
        gridView = new View(mContext);



        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
        return imageView;
    }

}

FacilityAdapter.class

DockerExtension

谢谢

2 个答案:

答案 0 :(得分:1)

我建议您不要在运行时动态创建ImageView并从getView()方法返回xml,而应为每个{{1}创建一个list items }。以下是xmlImageView

的示例TextView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"/>

</LinearLayout>

此外,您应该关注ViewHolder Pattern以提高GridView内存的效率。您的getView()方法看起来很相似

@Override
    public View getView(int arg0, View convertView, ViewGroup arg2) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.yourxml,
                    null);
            holder.textview= (TextView) convertView.findViewById(R.id.text);
            holder.imageview= (ImageView) convertView.findViewById(R.id.image);
            holder.imageView.getLayoutParams().width = yourImageWidth;
            holder.imageView.getLayoutParams().height = yourImageHeight;
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.imageView.setImageResource(mThumbIds[position]);
        holder.textview.setText(mThumbNames[position]);
        return convertView;
    }

    static class ViewHolder {
        ImageView imageView;
        TextView textView;
    }

答案 1 :(得分:0)

尝试扩展这个:

SimpleAdapter

或默认使用它,如:

private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
     R.drawable.bank, R.drawable.bus,
     R.drawable.girlshostel , R.drawable.guesthouse,
     R.drawable.gym , R.drawable.library,R.drawable.sports
};
public String[] mThumbNames = {"Bank", "Bus Service","Guest House", "GYM","Fac1","Fac2"};
Simple adapter= new SimpleAdapter(mContext, List<?> your_list, R.layout.your_layout_file, from, to);

您还必须制作一个布局文件,并定义您希望为每个gridview项目查找的图像和文本,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/rootLayout"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <ImageView
        android:id="@+id/this_image_id_have_to_be_used_in_your_to_array"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"/>

    <TextView
        android:id="@+id/this_text_id_have_to_be_used_in_your_to_array"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"/>

</LinearLayout>

希望它有所帮助!!!