在Android RelativeLayout中有没有一种方法可以填充父容器减去一个固定数值的空间?

36
我有一个 ImageButton,想要让它的宽度填满其父级 RelativeLayout 容器,但是减去一些 dp,以便左右留有一些内边距。我尝试使用 fill_parent-10dp,但这会导致错误并且无法呈现。

3
paddingmargin 怎么样? - K-ballo
2个回答

48

在 ImageButton 上加上 android:layout_margin="10dp",同时保留

android:layout_width="fill_parent"
android:layout_height="fill_parent"

fill_parent现在已经被弃用。 - MahNas92

10
  1. 使用xml属性android:layout_marginLeft来指定视图左侧额外的空间。
  2. 使用xml属性android:layout_marginRight来指定视图右侧额外的空间。
  3. 使用xml属性android:layout_marginTop来指定视图顶部额外的空间。
  4. 使用xml属性android:layout_marginBottom来指定视图底部额外的空间。
  5. 使用xml属性android:layout_margin来指定视图左、上、右、下四个方向额外的空间。

你的情况中,ImageButton声明应该是这样的:

<ImageButton
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="Button" 
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:src="@drawable/a"

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