底部边距或填充在 android 上的 xml 中的相对布局中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10237210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:56:41  来源:igfitidea点击:

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

androidimageviewpaddingmargin

提问by Atma

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

我有一个RelativeLayout在 a 里面的一行ListView。该行看起来像,

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

既不应用边距或填充ImageView. 如何创建这种类型的间距?

采纳答案by Android Stack

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.

希望这可以帮助。

回答by bkurzius

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

对于正在寻找解决方案的任何其他人 - 从文档中:

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

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

“请注意,RelativeLayout 的大小与其子项的位置之间不能有循环依赖关系。例如,不能有一个高度设置为 WRAP_CONTENT 且子项设置为 ALIGN_PARENT_BOTTOM 的 RelativeLayout。”

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

如果将 relativeView 放在滚动视图中,似乎也有一些奇怪的行为。

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.

就我而言,我已将 relativeLayout 设置为 wrap_content,然后尝试将 marginBottom 添加到具有 ALIGN_PARENT_BOTTOM 属性的 ImageView。当我明确设置高度时,它终于起作用了。

回答by amp

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

我建议您使用 LinearLayout ... 或者您可以尝试删除该属性: 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"