我正在使用Grid View for android制作应用程序。我在网格视图中获得了此代码,文本视图位于图像下方。但是没有显示文本视图。 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" />
答案 0 :(得分:1)
在getview
你有
return imageView;
因此显示其正常的唯一图像视图。在ImageView
中换行TextView
和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
答案 1 :(得分:1)
TextView
未展示,因为您仅在ImageView
方法中将getView()
作为视图返回。
您需要创建单独的布局,您必须返回包含ImageView
和TextView
的布局。
或
您可以动态创建LinearLayout
,然后添加ImageView
和TextView
并返回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;
}
答案 2 :(得分:0)
拿一个
ViewHolder holder;
holder = new ViewHolder();
和此ViewHolder对象中的图像和文本借助LayoutInflater并返回convertView作为getview的参数 在这里你只返回图像视图dude
在浏览了一些之后,我在这里找到了一个很好的例子: http://www.javacodegeeks.com/2013/08/android-custom-grid-view-example-with-image-and-text.html