2011-02-26 27 views
0

我有我的本地天鵝的一些圖片的galleryview,我想有一個隨圖片變化的textview。如何實現更改以匹配我的圖像在galleryview中的文本視圖?

我的工作代碼如下。目前我只有一個帶有圖像切換器的galleryview。 從我的谷歌搜索我認爲我需要使用一個數組適配器與文本字符串,但我一直沒能找到一種方法使其工作。正如你可能知道的那樣,我非常處於學習Java的早期階段!

任何幫助或建議指向我在正確的方向將非常感激。

感謝

馬克

public class MainMenuActivity extends Activity 
implements ViewFactory 
{  
//---the images to display--- 
Integer[] imageIDs = { 
     R.drawable.swan1, 
     R.drawable.swan2, 
     R.drawable.swan3, 
     R.drawable.swan4, 
     R.drawable.swan5, 
     R.drawable.swan6     
}; 

private ImageSwitcher imageSwitcher; 

@Override  
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.displayview); 

    imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1); 
    imageSwitcher.setFactory(this); 
    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, 
      android.R.anim.fade_in)); 
    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, 
      android.R.anim.fade_out)); 

    Gallery gallery = (Gallery) findViewById(R.id.gallery1); 
    gallery.setAdapter(new ImageAdapter(this)); 
    gallery.setOnItemClickListener(new OnItemClickListener() 
    { 
     public void onItemClick(AdapterView parent, 
     View v, int position, long id) 
     {     
      imageSwitcher.setImageResource(imageIDs[position]); 
     } 
    }); 
} 

public View makeView() 
{ 
    ImageView imageView = new ImageView(this); 
    imageView.setBackgroundColor(0xFF000000); 
    imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
    imageView.setLayoutParams(new 
      ImageSwitcher.LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.FILL_PARENT)); 
    return imageView; 
} 

public class ImageAdapter extends BaseAdapter 
{ 
    private Context context; 
    private int itemBackground; 

    public ImageAdapter(Context c) 
    { 
     context = c; 

     //---setting the style---     
     TypedArray a = obtainStyledAttributes(R.styleable.Gallery1); 
     itemBackground = a.getResourceId(
       R.styleable.Gallery1_android_galleryItemBackground, 0); 
     a.recycle();              
    } 

    //---returns the number of images--- 
    public int getCount() 
    { 
     return imageIDs.length; 
    } 

    //---returns the ID of an item--- 
    public Object getItem(int position) 
    { 
     return position; 
    } 

    public long getItemId(int position) 
    { 
     return position; 
    } 

    //---returns an ImageView view--- 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     ImageView imageView = new ImageView(context); 
     imageView.setImageResource(imageIDs[position]); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imageView.setLayoutParams(new Gallery.LayoutParams(150, 120)); 
     imageView.setBackgroundResource(itemBackground); 
     return imageView; 
    } 
    }  
    } 

我有我的數組中的字符串如下:

arrays.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string-array 
    name="swan"> 
    <item>swan_1</item> 
    <item>swan_2</item> 
    <item>swan_3</item> 
    <item>swan_4</item> 
    <item>swan_5</item> 
    <item>swan_6</item> 
    <item>swan_7</item> 
</string-array> 
</resources> 

繼哈坎給我的信息我知道我必須做什麼;我無法讓它工作。我一直在努力做這項工作,但是我的Java知識還是太糟糕了。任何進一步的幫助或將非常歡迎,這是我的頭痛。

displayview.xml:

<?xml version="1.0" encoding="utf-8"?> 
<merge xmlns:android="http://schemas.android.com/apk/res/android"> 
<ImageSwitcher 
    android:id="@+id/switcher1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"     
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true"  
    /> 
<Gallery 
    android:id="@+id/gallery1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="20dip" 
    /> 
<TextView 
    android:id="@+id/textview1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="10dip" 
    android:layout_gravity="center_horizontal|bottom" 
    android:background="#AA000000" 
    android:textColor="#ffffffff" 

    /> 
</merge> 

回答

0

您需要創建一個自定義佈局,並使用每個項目中包含的ImageView和一個TextView畫廊即佈局。這將是很好的使用FrameLayout。 Android已經給出了一個很好的例子和解釋here。一旦獲得佈局,您需要從getView方法中的佈局中獲取TextView對象並設置正確的消息。

要設置正確的消息,我會有一個字符串的ArrayList,然後在getView中,我將簡單地檢索ArrayList中「位置」處的字符串,並將其設置爲TextView的消息。

+0

非常感謝,現在就開始工作吧! – Mark 2011-03-03 21:46:15

+0

赫雅哈坎,我仍然堅持這一點。你的回答顯示了我必須做的事情,不幸的是我無法工作如何去做。我的谷歌福已經失敗了我。如果你或其他人願意幫助我,我會非常感激。非常感謝您花時間回覆! – Mark 2011-03-08 13:34:19

相關問題