Android:如何将父布局底部对齐 + 底部边距

73

我使用了相对布局,想将按钮设置在屏幕底部。但是这会把它放到底部,我希望在屏幕/视图的末尾和按钮之间留出一些空间。但无论我怎么做,按钮的外边距在2.1+上似乎都没有用。由于相对布局包含背景,所以我不能在其上设置外边距。

有人知道解决方法吗?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:background="@drawable/background" >

    <Button
        android:id="@+id/confirm_mobile_button_next"
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="false"
        android:layout_centerHorizontal="false"
        android:layout_centerInParent="false"
        android:layout_margin="15dp"
        android:background="@drawable/button_shape_selector"
        android:paddingLeft="10dip"
        android:paddingRight="10dip"
        android:text="@string/confirm_mobile_no_continue"
        android:textColor="@color/white"
        android:textStyle="bold" />

</RelativeLayout>
11个回答

-3

这里有另一种选择。如果你想设置子元素的边距而不是父元素的内边距,你应该将android:layout_marginTop的值设置为所需边距的两倍,然后将android:layout_centerVertical设置为true。顶部边距给定两倍的所需值以补偿底部边距。这样你就可以在子视图周围拥有相等的顶部和底部边距。

<Button
    android:id="@+id/confirm_mobile_button_next"
    android:layout_width="fill_parent"
    android:layout_height="40dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="false"
    android:layout_centerVertical="true"
    android:layout_centerInParent="false"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:layout_marginTop="30dp"
    android:background="@drawable/button_shape_selector"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:text="@string/confirm_mobile_no_continue"
    android:textColor="@color/white"
    android:textStyle="bold" />

它将会给你相同的结果。


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