首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓GalleryView问题

安卓GalleryView问题
EN

Stack Overflow用户
提问于 2011-06-28 16:18:08
回答 3查看 2.3K关注 0票数 0

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

代码语言:javascript
复制
        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;
    }
}

}

我该如何解决这个问题?

提前谢谢..

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-28 18:34:20

据我所知,您希望当前聚焦的图像占据所有屏幕空间。您可以通过设置绑定到图库的ImageView的宽度(在getView中)来实现这一点。尝试将图像的宽度设置为与设备屏幕的宽度相匹配。

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

我不能告诉你如何显示滚动条,对不起。

票数 0
EN

Stack Overflow用户

发布于 2011-06-28 16:35:22

您可以尝试返回宽度为fill parent的视图,其中包含来自适配器的图像视图getView() ...

或者你也可以这样做

getview函数

代码语言:javascript
复制
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

代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2011-06-28 16:49:05

我已经实现了类似的图库视图,在下面的链接中提到了它。也请尝试一下:Android gallery with caption

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6503544

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档