Align buttons to the bottom of the layout

流过昼夜 提交于 2019-12-25 18:43:43

问题


i want to align two buttons to the bottom of the screen as follows. i tried the xml given in this similar question but no luck.how to do it ?

like this -

this should be without the spacing in the bottom


回答1:


This is the property you need to use:

android:layout_alignParentBottom="true"

Try using the following XML and customize your button appearance as you like:

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



回答2:


I do not have the reputation necessary to comment on the response aashima

but I think you also have to add 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"/>




回答3:


You can also create an invisible variable to initialize the others on the layout .

For example, create an ImageView and make it invisible . After this center it vertically . You can also choose the height to match parent . So in the middle of the screen (vertically) there will be an ImageView . You can also set the width to 0,1 or more it depends on what you want and what kind of images you have

After you create it , for 'Previous' button you can choose AlignLeft option and choose this ImageView . For 'Next' button you can choose AlignRight and show this ImageView again .

With this trick the buttons always will be mirrored to each other on the screen .




回答4:


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


来源:https://stackoverflow.com/questions/31478856/align-buttons-to-the-bottom-of-the-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!