如何在Android中的GridView中设置图像显示中的TextView

时间:2013-03-12 04:50:31

标签: java android listview gridview

Hello EveryOne !!                    我在gridview中显示了图像。现在点击gridview中的任何图像我导航到下一页,屏幕上显示点击的图像。根据我的需要,我必须添加图像的描述在全屏幕上为我已经采取了TextView但它没有发生。这是我的代码...

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"/>

这是我的主要活动类代码..

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

}

1 个答案:

答案 0 :(得分:1)

这里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>