0

我正在尝试创建一个布局,其中包含一组水平居中的重叠扑克牌。重叠有效,但卡片未居中。如果我在卡片周围画了一个边框,那么相对布局似乎已经包括了如果没有应用负边距会使用的所有区域。然后将这个总面积居中。

如何确保相对布局的范围只是包含扑克牌 ImageViews 的区域,以便我可以水平居中扑克牌?

这是RelativeLayout定义

  <RelativeLayout
        android:id="@+id/tNorth"
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:layout_above="@+id/vCardTable"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="15sp"
        android:background="@drawable/losing_border"
        android:layout_marginBottom="15sp" >
    </RelativeLayout>

这是代码

    final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            if (isMiddle) {
                params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            }
            if (previousCard != null) {
                params.addRule(RelativeLayout.RIGHT_OF, previousCard.getId());
                params.setMargins(-pxPadding, 0, 0, 0);
            }
4

2 回答 2

0

您可以使用 ImageResource 和 ImageBackground 为带有填充的图片设置边框,与:

   <ImageView
        android:id="@+id/mImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name"
        android:padding="4dp"
        android:scaleType="fitCenter"
        android:background="@drawable/yourImageInCenter"
        android:src="@drawable/yourBorderForImage" />

此外,您可以在 hardCode 中使用此代码:

    params.setGravity(Gravity.CENTER);
于 2013-06-16T10:14:40.757 回答
0

我设法通过设置 ImageView 的宽度和高度来解决这个问题

            final int hPixels = (int) (100 * scale + 0.5f);
            final int wPixels = (int) (70 * scale + 0.5f);
            final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(wPixels, hPixels);

设置好布局后,我还可以删除 setAdjustViewBounds

于 2013-06-22T10:04:07.517 回答