在Android的XML中,相对布局中底部边距或填充不起作用。

11

我有一个放在 ListView 中的 RelativeLayout 行。这行看起来像这样:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1" 
    android:layout_width="wrap_content"
    android:layout_height="fill_parent">

    <ImageView
        android:id="@+id/placeDetailIcon_Img"
        android:layout_height="50dp"
        android:layout_width="50dp"
        android:layout_alignParentLeft="true" 
        android:paddingBottom="20dp"
        android:paddingTop="20dp" 
        android:paddingLeft="20dp"
        android:paddingRight="20dp"/>
</RelativeLayout>

我也尝试过使用margin:

 <ImageView
    android:layout_height="50dp"
    android:layout_width="50dp"
    android:id="@+id/placeDetailIcon_Img" 
    android:layout_alignParentLeft="true" 
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginBottom="10dp">

不要在ImageView周围应用边距或内边距。如何创建这种间距?


RelativeLayout android:layout_height="wrap_content"的意思是什么? - zapl
如果您的列表项只由一个视图表示,请摆脱RelativeLayout - MH.
3个回答

5
你可以使用这个,它对我很有效:

您可以使用此工具:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">  

     <ImageView
         android:id="@+id/imageView1"
         android:layout_width="50dp"
         android:layout_height="50dp"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:layout_marginLeft="15dp"
         android:layout_marginTop="15dp" />

</RelativeLayout>

希望这能帮到你。

为什么android:layout_marginTop有效而android:paddingTop无效? - user2512888

3
对于其他寻找此问题解决方案的人——文档上写着:注意RelativeLayout的大小与其子项的位置之间不能存在循环依赖关系。例如,您不能将高度设置为WRAP_CONTENT并且子项设置为ALIGN_PARENT_BOTTOM。此外,如果在ScrollView中放置RelativeLayout,也会出现一些奇怪的行为。在我的情况中,我将RelativeLayout设置为wrap_content,然后尝试为具有ALIGN_PARENT_BOTTOM属性的ImageView添加marginBottom。当我明确设置高度时,它最终起作用了。请参考:http://developer.android.com/reference/android/widget/RelativeLayout.html

0

我建议你使用LinearLayout... 或者你可以尝试删除属性:android:layout_alignParentLeft="true"

如果你想在RelativeLayout中居中视图,可以使用:

android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

2
android:layout_centerInParent="true" 可以一次完成这两个操作 :) - vm204

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接