画廊的观点并没有从左边开始(Gallery view is not starting from le

2019-09-16 08:35发布

我使用的库这样的

<Gallery
    android:id="@+id/gallery1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:spacing="2dp" >
</Gallery>

但是当我运行的代码我发现画廊从中间开始,我想从左边开始吧。 我应该为此做,请帮助我。

Answer 1:

刚刚成立画廊的选择下一个,这类似于该库在左侧位置。

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

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



Answer 2:

描述关于显示一般信息的结构,例如它的尺寸,密度,和字体scaling.To访问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);


Answer 3:

您应该使用为setSelection(1),但将其置于setAdapter后是很重要的()。 在另一种情况下,这是行不通的。

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


Answer 4:

你需要使用什么是.setSelection不.setSelected,下面的例子

廊画廊=(图库)findViewById(R.id.gallery);

gallery.setSelection(1);



Answer 5:

组的左画廊的利润率为负整数(比如说-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);


Answer 6:

试试这个它会工作.....

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

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

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



文章来源: Gallery view is not starting from left