2011-06-28 91 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