【问题标题】:Gallery view is not starting from left图库视图不是从左侧开始
【发布时间】:2012-04-05 10:25:37
【问题描述】:

我正在使用这样的画廊

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

但是当我运行代码时,我发现画廊从中间开始,我想从左边开始。我该怎么办,请帮帮我。

【问题讨论】:

标签: android android-gallery


【解决方案1】:

描述有关显示的一般信息的结构,例如其大小、密度和字体缩放。要访问 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);
    

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

    【讨论】:

      【解决方案3】:

      您应该使用 setSelection(1),但将它放在 setAdapter() 之后很重要。在另一种情况下它不起作用。

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

      【讨论】:

        【解决方案4】:

        您需要使用的是 .setSelection 而不是 .setSelected,示例如下

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

        gallery.setSelection(1);

        【讨论】:

          【解决方案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);
          

          【讨论】:

            【解决方案6】:

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

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

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

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-02-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-08-29
              • 1970-01-01
              相关资源
              最近更新 更多