bottom margin or padding doesn't work in relative layout in xml on android

痴心易碎 提交于 2019-12-10 01:45:55

问题


I have a RelativeLayout for a row that goes inside a ListView. The row looks like,

<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>

I also tried it with 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">

Neither apply the margin or padding around the ImageView. How do create this type of spacing?


回答1:


you can use this its work with me :

<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>

hope this helps.




回答2:


For anyone else who is looking for a solution to this - from the docs:

http://developer.android.com/reference/android/widget/RelativeLayout.html

"Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM."

It seems also that there is some odd behavior if you put a relativeView inside of a scrollview.

In my case I had set the relativeLayout to wrap_content and then tried to add marginBottom to an ImageView with the ALIGN_PARENT_BOTTOM attribute. When I set the height explicitly it finally worked.




回答3:


I suggest you to use LinearLayout... Or you can try to remove the attribute: android:layout_alignParentLeft="true"

If you want to center the view in a RelativeLayout you can use:

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


来源:https://stackoverflow.com/questions/10237210/bottom-margin-or-padding-doesnt-work-in-relative-layout-in-xml-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!