Android GalleryView问题

时间:2011-06-28 08:18:08

标签: android galleryview

我有一个android galleryview的问题,在我的应用程序中我有很多图片。我想通过画廊展示它们并使它们可以选择。我想出了将它们显示为水平可滚动并使它们可以用Galleryview选择,但我一次需要在屏幕上显示一个图像,在用户水平滚动滚动条之后,必须显示另一个图像。我的测试代码如下所示,屏幕上显示3张图片,来自http://www.androidpeople.com/android-gallery-example

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

}

我该如何解决这个问题?

提前致谢..

3 个答案:

答案 0 :(得分:0)

您可以尝试使用填充父级返回视图作为其宽度,其中包含来自适配器的图像视图getView()...

或者你也可以这样做

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>

答案 1 :(得分:0)

我已经实现了画廊视图,如下面链接中提到的那样。请尝试:Android gallery with caption

答案 2 :(得分:0)

据我了解,您希望当前聚焦的图像占用所有屏幕空间。您可以通过设置ImageView的宽度来将其绑定到Gallery(在getView中)来实现此目的。尝试设置图像的宽度以匹配设备屏幕的宽度。

请注意,您还可以使用gallery.setSpacing(...)方法设置图库中项目之间的间距。

我无法告诉你如何显示滚动条,抱歉。