首页 文章

Android:对齐父底部底部边距

提问于
浏览
61

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

  • -1

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

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

  • 64

    您可以做的另一件事是将 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>
    

    此示例使用 ImageView 填充 RelativeLayout ,并在 ImageView 上定位 TextView .

  • 3

    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>
    
  • 2

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

    <...
    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)
    
  • 29

    只有@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>
    

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

  • 15

    我认为最好的方法是在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)
    
  • 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>
    

    这应该对某些人有用 .

  • 3

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

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

    试试这种方式:

    <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" />
    
  • 1

    这是另一种选择 . 如果要设置子级's margin instead of parent'的填充,则应将 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" />
    

    它会给你相同的结果 .

相关问题