Android GalleryView问题

发布于 2024-11-17 09:14:11 字数 2738 浏览 2 评论 0原文

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

}

我该如何解决这个问题?

提前致谢..

I have a problem with android galleryview, in my app I have a lot of pictures. And I wanna display them by gallerview and make them selectable. I figured out displaying them horizontal scrollable and make them selectable with Galleryview, but I need one image on the screen at a time, after horizontal scrolling the scrollbar by the user, another picture must be shown. My test code is below and this shows 3 pictures on the screen, from 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;
    }
}

}

How can I solve this problem?

Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猥︴琐丶欲为 2024-11-24 09:14:11

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

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

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

As I understand it, you want the currently focused image to take up all the screen space. You can achieve this by setting the width of the ImageView where you bind it to the Gallery (in getView). Try setting the width of the image to match the width of the device screen.

Note that you can also set the spacing between the items in the Gallery with the gallery.setSpacing(...) method.

I can not tell you how to show a scrollbar, sorry.

遥远的她 2024-11-24 09:14:11

我已经实现了画廊视图,如下面链接中提到的那样。请也尝试一下:带标题的 Android 图库

I have implemented gallery view like, one that is mentioned in the below in the link. Please try that also: Android gallery with caption

橘和柠 2024-11-24 09:14:11

您可以尝试以填充父级作为其宽度返回视图,其中包含适配器 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>

You can try returning view with fill parent as its width which contains your imageview from your adapters getView() ...

Or alse you can do something like this

getview function

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文