【问题标题】:Text View is not shown in Grid View with images带有图像的网格视图中不显示文本视图
【发布时间】:2014-03-05 08:50:26
【问题描述】:

我正在为 android 制作带有 Grid View 的应用程序。我在图像下获得了带有文本视图的网格视图代码。但是没有显示文本视图。 Eclipse 没有向我显示任何错误或 logcat 中的任何内容

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    TextView text;
    private String[] mThumbTxt = {
            "Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5",
            "Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5",
            "Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5",
            "Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5",
            "Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5",
            "Example Text 1", "Example Text 2", "Example Text 3", " Example Text 4","Example Text 5",
            " Example Text 4","Example  5",


    };


// Keep all Images in array
public Integer[] mThumbIds = {
        R.drawable.img1, R.drawable.img2,
        R.drawable.img3,  R.drawable.img4, 
        R.drawable.img5,  R.drawable.img6, 
        R.drawable.img7,  R.drawable.img20, 
        R.drawable.img8,  R.drawable.img21, 
        R.drawable.img9,  R.drawable.img22, 
        R.drawable.img10,  R.drawable.img23, 
        R.drawable.img11,  R.drawable.img24, 
        R.drawable.img12,  R.drawable.img25, 
        R.drawable.img13,  R.drawable.img26, 
        R.drawable.img14,  R.drawable.img27, 
        R.drawable.img15,  R.drawable.img28, 
        R.drawable.img16,  R.drawable.img29, 
        R.drawable.img17,  R.drawable.img30, 
        R.drawable.img18,  R.drawable.img31, 
        R.drawable.img19
};

// Constructor
public ImageAdapter(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) {
    ImageView imageView = new ImageView(mContext);
    text=new TextView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setLayoutParams(new GridView.LayoutParams(270, 270));
    text.setText(mThumbTxt[position]);  
    return imageView;
}

这段代码有什么问题?我做错了什么? 单独的 XML 布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

【问题讨论】:

标签: android image gridview textview


【解决方案1】:

TextView 未显示,因为您仅在 getView() 方法中将 ImageView 作为视图返回。

您需要为此创建单独的布局,您必须返回包含ImageViewTextView 的布局。

您可以动态创建LinearLayout,在其中添加您的ImageViewTextView 并返回您的LinearLayout

  return layout;

尝试如下:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
     LinearLayout linearlayout=new LinearLayout(mContext);
     ImageView imageView = new ImageView(mContext);
     text=new TextView(mContext);
     linearlayout.setOrientation(LinearLayout.VERTICAL);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    imageView.setLayoutParams(new GridView.LayoutParams(270, 270));
    text.setText(mThumbTxt[position]); 

      linearlayout.addView(imageView);
      linearlayout.addView(text);
    return linearlayout;
}

【讨论】:

  • 是的,它正在工作,文本视图现在显示,但它在图像的右侧,我怎样才能让它在图像下方?
  • @user3094736 使用RelativeLayout。或将方向设置为垂直
  • 制作linearlayout.setOrientation(LinearLayout.VERTICAL);。 @user3094736
  • 哦,是的,现在它运行良好。谢谢!谢谢你们,我不能选择 2 个答案
【解决方案2】:

getview 你有

return imageView;

因此显示其正常的唯一图像视图。将 ImageViewTextView 包裹在 RelativeLayout/LinearLayout 中并返回视图。

或者使用 imageView 和 textView 扩展布局并返回视图。

编辑:

在构造函数中

   LayoutInflater mInflater; 
   public ImageAdapter(Context c){
       mInflater = LayoutInflater.from(c);
   }

然后在getView

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
  if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.yourlayout, 
                         parent, false);
      holder = new ViewHolder(); 
      holder.tv = (TextView) convertView.findViewById(R.id.textView1); 
      holder.iv = (ImageView) convertView.findViewById(R.id.imageView1);
      convertView.setTag(holder); 
       } else { 
           holder = (ViewHolder) convertView.getTag(); 
       } 
           holder.iv.setImageResource(mThumbIds[position]);
           holder.tv.setText(mThumbTxt[position]);
     return convertView;
 }

然后

static class ViewHolder
{
    TextView tv;
    ImageView iv;
} 

参考:

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

How ListView's recycling mechanism works

【讨论】:

  • @user3094736 则无需创建布局程序
  • 嗯,用户@GrlsHu 已经让它工作了,但它显示在图像的右侧而不是下方。
  • @user3094736 尝试更高效的编辑。 developer.android.com/training/improving-layouts/…
  • @user3094736 您需要将 linerlayout 方向设置为垂直。默认情况下它是水平的。顺便说一句,如果你膨胀你发布的布局,它应该有垂直方向android:orientation="vertical"
【解决方案3】:

拍一张

ViewHolder holder; 
   holder = new ViewHolder();

以及此 ViewHolder 对象中的图像和文本 wd 帮助 LayoutInflater 并将 convertView 作为 getview 的参数返回 在这里你只返回图像视图老兄

在环顾四周之后,我在这里找到了一个很好的例子来说明如何做到这一点: http://www.javacodegeeks.com/2013/08/android-custom-grid-view-example-with-image-and-text.html

【讨论】:

  • 不鼓励仅链接的答案。在您的答案中添加链接的内容或将链接发布为 cmets
猜你喜欢
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多