* *如何在网格视图中设置每个图像的下方文本
当我点击此文字时,我想在每张图片下方设置有关产品名称的文字,以便进入完整图片活动请帮帮我。这是我的代码
package com.a.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class AndroidGridActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
/**
* On Click event for Single Gridview Item
* */
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending text id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}`
这是android网格视图活动的布局文件
<TextView android:id="@+id/chooseText"
android:layout_height="wrap_content"
android:layout_width="85dp"
android:text="TextView"
android:layout_below="@+id/chooseImage"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:textColor="#3232FF">
</TextView>
<GridView
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"
android:background="#0000FF">
</GridView>
</RelativeLayout>
这是我的完整图像类
package com.example.androidhive;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
public class FullImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
// get intent data
Intent in = getIntent();
// Selected text id
int positiont = in.getExtras().getInt("id");
ImageAdapter imageAdaptert = new ImageAdapter(this);
TextView txtView = (TextView) findViewById(R.id.fulltext);
txtView.setText(imageAdapter.mThumbTxt[position]);
}
}
它是一个完整的图像布局代码
<ImageView android:id="@+id/full_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"/>
<TextView
android:id="@+id/fulltext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#000000" />
</LinearLayout>
其图像适配器类
package com.example.androidhive;
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.RelativeLayout;
import android.widget.TextView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9, R.drawable.pic_10,
R.drawable.pic_11, R.drawable.pic_12,
R.drawable.pic_13, R.drawable.pic_14,
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
public String[] mThumbTxt = {
"Müzeler", "Anıtlar", "İbadethaneler", "Hepsi","iqra","rafia","sana","sobia","amna","aisha","asma","asna","urosa","asnam"
};
public View getView1(int position, View convertView, ViewGroup arg2) {
View view = View.inflate(mContext, R.layout.gridview, null);
RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.relaGrid);
ImageView image = (ImageView) rl.findViewById(R.id.chooseImage);
TextView text = (TextView) rl.findViewById(R.id.chooseText);
image.setImageResource(mThumbIds[position]);
text.setText(mThumbTxt[position]);
return rl;
}
}
答案 0 :(得分:0)
参考下面的代码
GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
sdcardImages.setAdapter(new ImageAdapter(myImages, myViews));
sdcardImages.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
Intent i = new Intent(EquipmentViews.this, VIRDemo.class);
i.putExtra("EquipmentId", equipmentIds[position]);
startActivity(i);
}
});
然后创建适配器
private class ImageAdapter extends BaseAdapter {
private int[] myRemoteImages;
private String[] myRemoteViews;
public ImageAdapter(int[] myimages, String[] myviews) {
myRemoteImages = myimages;
myRemoteViews = myviews;
}
public int getCount() {
return this.myRemoteImages.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.imageitem, null);
TextView tv = (TextView) v.findViewById(R.id.icon_text);
tv.setText(myRemoteViews[position]);
ImageView iv = (ImageView) v.findViewById(R.id.icon_image);
iv.setImageResource(myRemoteImages[position]);
} else {
v = convertView;
}
return v;
}
}
XML文件imageitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="center_horizontal" >
<ImageView android:id="@+id/icon_image" android:layout_width="200dp"
android:layout_height="150dp">
</ImageView>
<TextView android:id="@+id/icon_text" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="TextView"
android:gravity="center_horizontal" android:textColorHighlight="#656565">
</TextView>
</LinearLayout>