Android:对齐父级底部+底部边距

Jac*_*cob 64 android margins android-relativelayout

我已经使用了相对布局,我想在屏幕底部设置按钮,但是这会把它全部放到底部,我想有一些余量,所以它在屏幕的末端之间有一些空间/查看和按钮.无论我做什么,按钮边距在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>
Run Code Online (Sandbox Code Playgroud)

And*_*dré 70

您可以简单地向RelativeLayout添加填充而不是Button的边距,例如android:paddingBottom="15dp".

一般来说,我总是使用API​​ Level 8设置在Exclipse预览中测试我的布局.这为大多数设备提供了非常准确的结果,包括ICS和JB.

  • 这对我来说没有,因为我使用的MapView应该涵盖所有屏幕.我只是使用`android:layout_alignParentBottom ="true"`添加一个具有所需高度的空白`TextView`并在上面放置所需的对象(在这种情况下为`Button`). (11认同)
  • 当textview/button具有背景时,此解决方案无效,因为它的大小受paddingBottom影响,并且看起来很糟糕.最好在底部放置一个看不见的视图 (3认同)

aja*_*n81 31

你可以做的另一件事就是把View它对齐到底部RelativeLayout,并将它的高度设置为你想要使用的底部边距(或者只是指定一个值layout_marginBottom),如下所示:

 <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    > 
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/some_image"
        android:adjustViewBounds="true"

         />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some Overlay"
        android:padding="3dip"
        android:gravity="center_vertical|center_horizontal"

        android:layout_above="@+id/empty_view"
        android:layout_marginBottom="35dip"
        />
    <View 
        android:id = "@+id/empty_view"
        android:layout_height = "30dip"
        android:layout_width = "match_parent"
        android:visibility="invisible"
        android:layout_alignParentBottom="true"
        />

    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

这个例子填充RelativeLayoutImageView,并定位TextViewImageView.

  • 使用Space而不是不可见的View (5认同)

Shu*_*ham 18

Yu可以使用translateY属性

translateY="-16dp"
Run Code Online (Sandbox Code Playgroud)

最终代码

<?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"
        android:translateY="-16dp" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)