7

我有一个带有 2 个图像的内部相对布局,当我设置 android:layout_marginRight 属性时,它什么也不做。

这是我的代码:

<RelativeLayout
    android:id="@+id/location_image_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="80dp" />

    <ImageView
        android:layout_width="69.33dp"
        android:layout_height="72dp"
        android:background="@drawable/icon"
        android:layout_centerInParent="true" />

</RelativeLayout>
4

5 回答 5

24

问题出在布局的属性android:layout_width中。当它设置为"wrap_content"时,android:layout_marginRight将不起作用,而是只有当它设置为时"fill_parent"android:layout_marginRight才会起作用。

于 2012-10-30T04:34:16.143 回答
1

您使用了以下内容,这就是它发生的原因

android:layout_alignParentRight="true"

只需删除此行,您将在图像视图中从右侧获得边距..

于 2012-10-28T14:32:45.600 回答
1
<LinearLayout
    android:id="@+id/location_image_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="80dp" />

    <ImageView
        android:layout_width="69.33dp"
        android:layout_height="72dp"
        android:background="@drawable/icone"
        android:layout_centerInParent="true" />

</LinearLayout>

试试这个。它对我有用,并且完全符合您的要求

于 2012-10-28T18:10:29.907 回答
1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    ...
/>

我遇到了同样的问题,如果您将父级 RelativeLayout 的 layout_width 更改为“match_parent”,它就可以工作。希望它也适合你。

于 2013-05-22T03:28:00.330 回答
-4

尝试使用 LinearLayout 而不是 RelativeLayout 在布局中应用 Margin。

于 2012-10-29T05:14:25.750 回答