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

时间:2012-11-18 15:51:37

标签: android android-relativelayout margins

我已经使用了相对布局,我想在屏幕底部设置按钮,但是这将它全部放到底部,我希望有一些余量,所以它之间有一些空间屏幕/视图和按钮。无论我做什么,按钮边距在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>

10 个答案:

答案 0 :(得分:66)

您只需向RelativeLayout添加填充,而不是向Button添加边距,例如android:paddingBottom="15dp"

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

答案 1 :(得分:30)

您可以做的另一件事是将ViewRelativeLayout的底部对齐,并将其高度设置为您想要使用的下边距(或者只是指定一个值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>

此示例使用RelativeLayout填充ImageView,并将TextView放在ImageView上。

答案 2 :(得分:15)

Yu可以使用translateY属性

translateY="-16dp"

最终代码

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

答案 3 :(得分:3)

我认为最好的方法是设置:

<...
android:layout_alignParentBottom="true" />

然后在java代码后面:

Button confirm_mobile_button_next = (Button)findViewById(R.id.confirm_mobile_button_next)
confirm_mobile_button_next.setTransitionY(-THE_VALUE) or setTransitionX(-THE_VALUE)

答案 4 :(得分:3)

@franny zhao建议的唯一工作解决方案如下。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:id="@+id/fl_layout"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_layout"
            android:text="hello world"
            android:layout_marginBottom="50dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </FrameLayout>
</RelativeLayout>

如果您按照其他人的建议将填充添加到底部对齐视图,则视图背景也会延伸。如果你有彩色背景,那么视图看起来就像粘在底部。填充和边距完全不同,填充是视图的一部分,但边距在视图之间留下空间。

答案 5 :(得分:2)

我认为最好的方法是在XML中设置android:layout_alignParentBottom 然后在java代码后面:

Button confirm_mobile_button_next = (Button)findViewById(R.id.confirm_mobile_button_next)

confirm_mobile_button_next.setTransitionY(-THE_VALUE) or setTransitionX(-THE_VALUE)

答案 6 :(得分:1)

由于我偶然发现了这个问题,并且没有找到适合我情况的答案,我开始思考半秒钟并通过在RelativeLayout示例中的视图上设置负边距来解决它:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
    android:layout_width="48dp"
    android:layout_height="48dp"
        android:layout_marginTop="-8dp"
        android:layout_marginStart="-8dp">
</RelativeLayout>

这应该对某些人有用。

答案 7 :(得分:1)

您可以使用ViewGroup(例如,FrameLayout或LinearLayout)来包装视图。在外部ViewGroup中设置alignParentBottom,然后marginBottom可以在内部视图中工作。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:id="@+id/fl_layout"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/tv_layout"
            android:text="hello world"
            android:layout_marginBottom="50dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </FrameLayout>
</RelativeLayout>

答案 8 :(得分:-1)

尝试这种方式:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="17dp"
    android:text="Button" />

答案 9 :(得分:-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" />

它会给你相同的结果。

相关问题