【问题标题】:Android GalleryView ProblemAndroid GalleryView 问题
【发布时间】:2011-06-28 18:40:43
【问题描述】:

我在使用 android galleryview 时遇到问题,在我的应用中我有很多图片。我想通过gallerview显示它们并使它们可选择。我想用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;
    }
}

}

我该如何解决这个问题?

提前谢谢..

【问题讨论】:

    标签: android galleryview


    【解决方案1】:

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

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

    我不能告诉你如何显示滚动条,抱歉。

    【讨论】:

    • 我只需要间距!画廊.setSpacing(100);然后我一次在屏幕上看到一个图像。另外我不需要可见的滚动条,如果发生触摸事件,android os 会为我处理滚动,特别感谢...
    【解决方案2】:

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

    【讨论】:

    • 你的实现和我的没有什么不同。我需要一个滚动条和一个屏幕上的图像。
    【解决方案3】:

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

    galaryitem.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>
    

    【讨论】:

      猜你喜欢
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多