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



大家好!我已经在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"/>

和这是我的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是height和width的fill parent,所以它占用了屏幕上所有可用的空间。因此,你需要设计你的布局,这样的方式是显示这两个元素的顺序。

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

最新更新