【问题标题】:bottom margin or padding doesn't work in relative layout in xml on android底部边距或填充在 android 上的 xml 中的相对布局中不起作用
【发布时间】:2012-05-01 12:09:39
【问题描述】:

我有一个 RelativeLayout 用于在 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>

我也试过用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"?
  • 如果您的列表项将仅由一个视图表示,则还要去掉 RelativeLayout

标签: android imageview padding margin


【解决方案1】:

你可以和我一起使用它:

<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 无效?
【解决方案2】:

对于其他正在寻找解决方案的人 - 来自文档:

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

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

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

在我的例子中,我将 relativeLayout 设置为 wrap_content,然后尝试使用 ALIGN_PARENT_BOTTOM 属性将 marginBottom 添加到 ImageView。当我明确设置高度时,它终于起作用了。

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

    • android:layout_centerInParent="true" 一次性完成这两项工作:)
    猜你喜欢
    • 2015-12-02
    • 1970-01-01
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    相关资源
    最近更新 更多