2016-02-14 66 views
0

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

fragment_facility_grid.xml

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

</GridView> 

FacilityAdapter.class

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; 
    } 

} 

谢谢

+0

http://www.androidhub4you.com/2013/07/custom-grid-view-example-in-android.html –

回答

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> 

希望它有助于!!!

+0

如何ü可以只输入并显示XML文件,以便我可以得到一个主意 –

+0

检查我编辑的答案 –

1

我宁愿建议,而不是在运行时创建ImageView动态,并返回从getView()方法,你应该为你的list items的每一个xml。下面是示例xmlImageViewTextView

<?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; 
    }