2011-06-28 93 views
0

我與Android一圖庫視圖問題,在我的應用我有很多的圖片。我想通過gallerview顯示它們並使它們可選。我想通過水平滾動顯示它們,並使用Galleryview選擇它們,但是我一次需要在屏幕上顯示一個圖像,在用戶水平滾動滾動條之後,必須顯示另一張圖像。我的測試代碼如下,這顯示在屏幕上3張圖片,從http://www.androidpeople.com/android-gallery-exampleAndroid的問題圖庫視圖

 package com.projects.cards; 

    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.AdapterView.OnItemClickListener; 
    import android.widget.BaseAdapter; 
    import android.widget.Gallery; 
    import android.widget.ImageView; 
    import android.widget.Toast; 

public class GaleryTestActivity extends Activity { 

private Gallery gallery; 

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

    gallery = (Gallery) findViewById(R.id.examplegallery); 
    gallery.setAdapter(new AddImgAdp(this)); 

    gallery.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, long id) { 
      // Displaying the position when the gallery item in clicked 
      Toast.makeText(GaleryTestActivity.this, "Position=" + position, Toast.LENGTH_SHORT).show(); 
     } 
    }); 

} 

public class AddImgAdp extends BaseAdapter { 
    int GalItemBg; 
    private Context cont; 

    // Adding images. 
    private Integer[] Imgid = { 
      R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7 
    }; 

    public AddImgAdp(Context c) { 
     cont = c; 
     TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme); 
     GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0); 
     typArray.recycle(); 
    } 

    public int getCount() { 
     return Imgid.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

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

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imgView = new ImageView(cont); 

     imgView.setImageResource(Imgid[position]); 
     // Fixing width & height for image to display 
     imgView.setLayoutParams(new Gallery.LayoutParams(200, 160)); 
     imgView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imgView.setBackgroundResource(GalItemBg); 

     return imgView; 
    } 
} 

}

我怎樣才能解決這個問題?

在此先感謝..

回答

0

據我瞭解,你想當前聚焦圖像佔用所有的屏幕空間。您可以通過設置ImageView的寬度來將其綁定到Gallery(在getView中)。嘗試設置圖像的寬度以匹配設備屏幕的寬度。

請注意,您也可以使用gallery.setSpacing(...)方法設置Gallery中的項目之間的間距。

我不能告訴你如何顯示滾動條,對不起。

+0

我需要的只是間隔! gallery.setSpacing(100);然後我在屏幕上看到一個圖像。此外,我不需要一個可見的滾動條,如果觸摸事件發生,Android操作系統處理滾動我,特別感謝... – barisatbas

0

你可以嘗試用填充母其寬度包含從適配器getView()的返回imageview的看法...

或者ALSE你可以做這樣的事情

getview功能

public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater iteminflater = LayoutInflater.from(cont); 
    View gallaryitem = iteminflater .inflate(R.layout.gallaryitem, null); 
    ImageView imgView= (ImageView) gallaryitem .findViewById(R.id.image); 
    imgView.setImageResource(Imgid[position]); 
    gallaryitem .setBackgroundResource(GalItemBg); 
    return gallaryitem ; 
    } 

gallaryitem.xml

<LinearLayout android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       xmlns:android="http://schemas.android.com/apk/res/android" >  
     <LinearLayout android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        android:layout_gravity="center_horizontal" 
        xmlns:android="http://schemas.android.com/apk/res/android" > 

      <ImageView android:layout_width="wrap_content" 
         android:id="@+id/image" 
         android:layout_height="wrap_content" 
         android:src="@drawable/icon" 
         xmlns:android="http://schemas.android.com/apk/res/android" > 

      </ImageView> 
     </LinearLayout> 
</LinearLayout> 
+0

我需要哪種方法來使用? imgView ???(); – barisatbas

0

我已實施等,通過在下面的鏈接中提到一個畫廊視圖。請嘗試也:Android gallery with caption

+0

您的實施與我的沒有區別。我需要在屏幕上一次一個滾動條和一個圖像。 – barisatbas