将按钮对齐到布局的底部

时间:2015-07-17 15:02:26

标签: android android-layout uielement

我想将两个按钮对齐到屏幕底部,如下所示。我尝试了这个similar question中给出的xml,但没有运气。如何做到这一点?

像这样 -

enter image description here

这应该没有底部的间距

enter image description here

4 个答案:

答案 0 :(得分:4)

这是您需要使用的属性:

android:layout_alignParentBottom="true"

尝试使用以下XML并根据需要自定义按钮外观:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d1000000"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="PREV"
        android:layout_marginRight="-5dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="NEXT"
        android:layout_marginLeft="-5dp"/>

</LinearLayout>
</RelativeLayout>

答案 1 :(得分:2)

我没有必要的评论回应aashima

但我认为您还必须添加android:weightSum="2"

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_alignParentBottom="true"
android:orientation="horizontal">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="PREV"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="NEXT"/>

答案 2 :(得分:0)

您还可以创建一个不可见变量来初始化布局中的其他变量。

例如,创建一个ImageView并使其不可见。在这个中心之后垂直。您也可以选择与父级匹配的高度。所以在屏幕中间(垂直)会有一个ImageView。您还可以将宽度设置为0,1或更多,这取决于您想要的和您拥有的图像种类

创建后,对于“上一页”按钮,您可以选择AlignLeft选项并选择此ImageView。对于“下一步”按钮,您可以选择AlignRight并再次显示此ImageView。

通过这个技巧,按钮总是会在屏幕上相互镜像。

答案 3 :(得分:-2)

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:gravity="bottom|center"
    >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:onClick="before"
        android:layout_toLeftOf="@+id/i"
        android:layout_gravity="bottom|left" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:onClick="after"
        android:layout_gravity="bottom|right"/>

    </LinearLayout>
 </RelativeLayout>
相关问题