2012-08-16 56 views
0

我有一個GalleryView與一些圖片,我想有一個TextView隨圖片變化。任何人都可以告訴如何在每張圖片中添加說明。我是Android新手。 任何幫助或指導如何添加關於圖庫中的每個圖像的描述

這是我的代碼:

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

<Gallery 
android:id="@+id/Gallery01" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"></Gallery> 
<ImageView 
android:id="@+id/ImageView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"></ImageView> 


</LinearLayout> 

的java:

import android.app.Activity; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.ImageView; 
import android.widget.Toast; 
import android.widget.AdapterView.OnItemClickListener; 

public class GalleryView extends Activity { 
Integer[] pics = { 
     R.drawable.antartica1, 
     R.drawable.antartica2, 
     R.drawable.antartica3, 
     R.drawable.antartica4, 
     R.drawable.antartica5, 
     R.drawable.antartica6, 
     R.drawable.antartica7, 
     R.drawable.antartica8, 
     R.drawable.antartica9, 
     R.drawable.antartica10 
}; 
ImageView imageView; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Gallery ga = (Gallery)findViewById(R.id.Gallery01); 
    ga.setAdapter(new ImageAdapter(this)); 

    imageView = (ImageView)findViewById(R.id.ImageView01); 
    ga.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      Toast.makeText(getBaseContext(), 
        "You have selected picture " + (arg2+1) + " of Antartica", 
        Toast.LENGTH_SHORT).show(); 
      imageView.setImageResource(pics[arg2]); 

     } 

    }); 

} 


public class ImageAdapter extends BaseAdapter { 

    private Context ctx; 
    int imageBackground; 

    public ImageAdapter(Context c) { 
     ctx = c; 
     TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1); 
     imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1); 
     ta.recycle(); 
    } 

    public int getCount() { 

     return pics.length; 
    } 

    public Object getItem(int arg0) { 

     return arg0; 
    } 

    public long getItemId(int arg0) { 

     return arg0; 
    } 

    public View getView(int arg0, View arg1, ViewGroup arg2) { 
     ImageView iv = new ImageView(ctx); 
     iv.setImageResource(pics[arg0]); 
     iv.setScaleType(ImageView.ScaleType.FIT_XY); 
     iv.setLayoutParams(new Gallery.LayoutParams(150,120)); 
     iv.setBackgroundResource(imageBackground); 
     return iv; 
    } 

} 
} 
+0

你想連接到它的每個圖像在畫廊或外廊單TextView的改變各自選定的圖片下面一個TextView? – Sayyam 2012-08-16 17:22:10

+0

是的,在圖庫中,我希望textview可以根據不同的選擇性image.please引導我在正確的方向。 – rameesha 2012-08-17 11:16:35

回答

0

如果你想有一個TextView改變它的文本時選擇在圖像在Gallery。此TextView將在Galllery之外,並且您將在選擇圖庫項目時更改其內容。下面的代碼可能會給你一個提示。

XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

<Gallery 
android:id="@+id/Gallery01" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"></Gallery> 
<ImageView 
android:id="@+id/ImageView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"></ImageView> 

<TextView 
android:id="@+id/TextView01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_below="@+id/ImageView01"></TextView > 


</LinearLayout> 

在Java代碼中,你必須設置一個onIteSelectedListener()將更新TextView

1)初始化TextView

TextView textView = (TextView)findViewById(R.id.TextView01); 

2)設置onitemSelectedListener() of Gallery

ga.setOnItemClickListener(galleryOnItemSelectedListener); 

3)你的OnItemSelectedListener()會是這樣:

OnItemSelectedListener galleryOnItemSelectedListener = new OnItemSelectedListener() { 

    @Override 
    public void onItemSelected(AdapterView<?> arg0, View view, int position, 
      long arg3) { 
      // update the TextView here depending on the position of item selected 
      // from a description array or tag value from view. 
      // i.e texteView.setText(descriptions[position]); 

    } 

    @Override 
    public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 

    } 
}; 
+0

您想在Gallery或我們剛剛創建的TextView中添加這些屬性的位置? – Sayyam 2012-08-17 20:36:01

相關問題