画廊视图不从左侧开始启动。

4
我正在这样使用Gallery
<Gallery
    android:id="@+id/gallery1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:spacing="2dp" >
</Gallery>

当我运行代码时,发现图库从中间开始显示,我想让它从左边开始显示,请问我该怎么做?请帮助我。


你是否正在使用适配器?如果是,能否发布您的适配器代码? - Deva
这篇文章将会帮助你: http://stackoverflow.com/questions/4341158/android-align-first-item-in-gallery-to-the-left - Wim Strouven
6个回答

7

一个描述显示器通用信息的结构,比如大小、密度和字体缩放。要访问DisplayMetrics成员,可以按以下方式初始化一个对象。

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        Gallery g = (Gallery) findViewById(R.id.gallery);

        // set gallery to left side
        MarginLayoutParams mlp = (MarginLayoutParams) g.getLayoutParams();
        mlp.setMargins(-(metrics.widthPixels / 2 + (imageWidth/2)), mlp.topMargin,
                    mlp.rightMargin, mlp.bottomMargin);

2

只需将图库的选择设置为下一个,这样就相当于将图库放在左侧位置。

Gallery mGallery= (Gallery) findViewById(R.id.gallery);
mGallery.setSelection(1);

然后继续你的正常工作 :)

2

你应该使用setSelection(1),但重要的是在setAdapter()之后放置它。否则它将无效。

Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new GalleryImageAdapter(this));
gallery.setSelection(1);

1
您需要使用的是.setSelection而不是.setSelected,以下是示例:
Gallery gallery= (Gallery) findViewById(R.id.gallery);

gallery.setSelection(1);


0

尝试这个,它会起作用的...

Gallery g=(Gallery)findViewById(R.id.gallery1);

MarginLayoutParams mlp=(MarginLayoutParams)g.getLayoutParams();

mlp.setMargins(-200, 0, 0, 0);


0

将画廊的左边距设置为负整数(比如-80个dip)。为了正确计算,您需要在运行时检查屏幕宽度,然后根据您的项目(图像)宽度进行以下操作:

    int offset = width/2 - itemWidth/2; // you may add your spacing here too
    MarginLayoutParams mlp = (MarginLayoutParams) getLayoutParams();
    mlp.setMargins(-offset, mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接