如何在GridView中设置图像显示中的TextView在Android中
问题描述:
Hello EveryOne !! 我已经在gridview中显示图像。现在点击从gridview中的任何图像我正在导航到屏幕上显示的点击图像的下一页。根据我的需要,我必须添加图像的描述在全屏我采取了一个TextView但它不是happening.Here是我的代码...如何在GridView中设置图像显示中的TextView在Android中
FullImage.java
public class FullImage extends Activity {
private TextView lView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
if (position == 1){
lView = (TextView)findViewById(R.id.my_title);
lView.setText("Akshardham");
}
else{
lView = (TextView)findViewById(R.id.my_title);
lView.setText("Other");
}
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
而且Full.xml
<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/full_image_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView
android:id="@+id/my_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dip"/>
这里是我的MainActivity类别代码..
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), FullImage.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
,这里是我的ImageAdapter代码...
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.akshar, R.drawable.charminar,
R.drawable.gateway, R.drawable.goa,
R.drawable.jimcorbet, R.drawable.kerala,
R.drawable.mussoorie, R.drawable.parliament,
R.drawable.qutub, R.drawable.taj,
R.drawable.tirupati
};
// 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);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(270, 270));
return imageView;
}
}
答
这里ImageView的就是填写家长的高度和宽度,以便它会占用屏幕上的所有空间。所以你需要设计你的布局,这样就显示了这两个元素的顺序。
<RelativeLayout
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/full_image_view"
android:layout_width="fill_parent"
android:src="@drawable/button"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/my_title"
android:layout_centerHorizontal="true"
android:layout_below="@+id/full_image_view"
android:layout_height="wrap_content"
android:text="This is the image which i want at bottom"
android:layout_width="wrap_content"
android:layout_marginLeft="5dip" />
</RelativeLayout>
+0
Thanx Sir for ur response ... It Worked .. – vikas 2013-03-12 05:09:09
你是如何传递意图? – 2013-03-12 04:53:12
粘贴您的ImageAdapter代码。我认为这个问题是你对Java如何工作的误解。 – Cristian 2013-03-12 04:55:07
@Hardik Thanx先生为我的回应我用意向代码更新了我的代码。 – vikas 2013-03-12 04:56:30