【问题标题】:Gallery images are starting from center of gallery in android画廊图像从 android 画廊的中心开始
【发布时间】:2013-02-15 09:38:11
【问题描述】:

我在两个按钮之间有一个画廊,如下所示在我的 xml 文件中。在图库中,图像是从中心添加的。

<RelativeLayout
      android:layout_height="80dp"
      android:layout_width="fill_parent"
      android:id="@+id/gal"
      android:layout_alignParentBottom="true"
      android:background="@drawable/backgroundblackwhite">
       <Button android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:background="@drawable/backward"
           android:layout_alignParentLeft="true"/>
       <Button android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:background="@drawable/forward"
           android:layout_alignParentRight="true"/>
      <Gallery 
          android:layout_height="100dp"
          android:layout_width="fill_parent"
          android:layout_alignParentBottom="true"
          android:id="@+id/gallary"
          android:layout_marginLeft="100dp"
          android:layout_marginRight="100dp"
          android:spacing="5dp"
          android:layout_marginBottom="10dp"
          android:background="@drawable/greypattren"/>  
   </RelativeLayout>

I want the images to start from Left of my Gallery. I used the below code to set the gallery images to start from the left of gallery view.

DisplayMetrics 指标 = 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);

但是,我的 Gallery 将向左移动,即将我的 Button 隐藏在左侧(我应该使用它来滚动 Gallery)

我发布了两张图片的截图, 1)我想要的图库视图,我希望从屏幕左侧添加图像。 2)使用指标添加上述代码(设置边距)后更改图库。

我需要更改 xml 或代码中的任何内容吗?

请帮忙 提前致谢

【问题讨论】:

  • 嗨,你有答案吗?

标签: android android-layout android-relativelayout


【解决方案1】:

替换

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

mlp.setMargins((int) -(metrics.widthPixels/2.5), mlp.topMargin, mlp.rightMargin,                    mlp.bottomMargin);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多