简体   繁体   English

将按钮与屏幕LinearLayout的底部对齐

[英]Aligning buttons to buttom of screen LinearLayout

I have this layout: 我有这样的布局:

    <LinearLayout 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:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="66dp"
            android:layout_height="64dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/office_girl" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/greetingTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hi Boss,"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/dailyStatusTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="What to say if someone call?" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >
<!--         android:layout_gravity="center" -->

        <EditText
            android:id="@+id/suto_sms_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:hint="Text"
            android:inputType="textMultiLine"
            android:lines="3" />

    </LinearLayout>

   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <TextView
                android:id="@+id/text_phone_status"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Phone Status"
                android:layout_marginTop="3dp"
                android:layout_marginLeft="5dp"
                android:textSize="20sp" />
        <Spinner
            android:id="@+id/spinner_phone_status"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"/>

    </LinearLayout>

   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <TextView
                android:id="@+id/text_busy_until"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="until"
                android:layout_marginTop="3dp"
                android:layout_marginLeft="5dp"
                android:textSize="20sp" />
        <Button
            android:enabled="true"
            android:id="@+id/button_set_date_and_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_marginLeft="30dp"
            android:text="@string/set_time" />
        <TextView
                android:enabled="true"
                android:id="@+id/text_date_and_time"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="3dp"
                android:layout_marginLeft="40dp"
                android:textSize="12sp" 
                android:textStyle="bold"/>


    </LinearLayout>

   <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/button_confirm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:text="@string/button_confirm" />
        <Button
            android:id="@+id/button_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_marginLeft="3dp"
            android:text="@string/button_cancel" />



    </LinearLayout>

</LinearLayout>

This is part of the output: 这是输出的一部分:

在此处输入图片说明

My question is how to align my Confirm and Cancel buttons (last part of the xml) to buttom of the screen? 我的问题是如何将“确认”和“取消”按钮(xml的最后一部分)与屏幕的底部对齐?

I saw examples of how it can be done using relative layout, but my xml is quite big and i dont want to have to change it to relative layout as it will mean lots of work. 我看到了如何使用相对布局完成示例,但我的xml很大,我不想将其更改为相对布局,因为这将意味着很多工作。

Thanks. 谢谢。

在按钮及其包含LinearLayout上使用android:layout_gravity="bottom"属性。

Add

android:layout_weight="1" 

in second last LinearLayout and 在倒数第二个LinearLayout中,

android:layout_weight="0"

in last LinearLayout. 在最后的LinearLayout中。 Hope this work for you. 希望这项工作对您有用。

Try this code 试试这个代码

<LinearLayout 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:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.9"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="66dp"
            android:layout_height="64dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/greetingTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hi Boss,"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/dailyStatusTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="What to say if someone call?" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >

        <!-- android:layout_gravity="center" -->

        <EditText
            android:id="@+id/suto_sms_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:hint="Text"
            android:inputType="textMultiLine"
            android:lines="3" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/text_phone_status"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="3dp"
            android:text="Phone Status"
            android:textSize="20sp" />

        <Spinner
            android:id="@+id/spinner_phone_status"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/text_busy_until"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="3dp"
            android:text="until"
            android:textSize="20sp" />

        <Button
            android:id="@+id/button_set_date_and_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="3dp"
            android:enabled="true"
            android:text="set_time" />

        <TextView
            android:id="@+id/text_date_and_time"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_marginTop="3dp"
            android:enabled="true"
            android:textSize="12sp"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.025"
    android:baselineAligned="true"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button_confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="confirm" />

    <Button
        android:id="@+id/button_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cancel" />
</LinearLayout>

And here your using lot of LinearLayouts but personally i recommend you to try out relativelayout 这是您使用大量LinearLayouts的地方,但我个人建议您尝试使用relativelayout

It's actually very easy to use the RelativeLayout, because you can be very smart about it. 使用RelativeLayout实际上非常容易,因为您可以非常聪明地使用它。 For example, wrap it around everything, so that your current parent linear layout will be the first child, end put the linear layout with the buttons at outside of the current parent with the parameter alignParentBottom 例如,将其包装在所有内容周围,以使当前父级线性布局成为第一个子级,最后将线性布局的按钮置于父级当前父级的外部,并带有alignParentBottom参数

Please see this: 请看这个:

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

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="66dp"
            android:layout_height="64dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/plus" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/greetingTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hi Boss,"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/dailyStatusTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="What to say if someone call?" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal" >
        <!--         android:layout_gravity="center" -->

        <EditText
            android:id="@+id/suto_sms_text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="3dp"
            android:hint="Text"
            android:inputType="textMultiLine"
            android:lines="3" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/text_phone_status"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Phone Status"
            android:layout_marginTop="3dp"
            android:layout_marginLeft="5dp"
            android:textSize="20sp" />
        <Spinner
            android:id="@+id/spinner_phone_status"
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dp"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/text_busy_until"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="until"
            android:layout_marginTop="3dp"
            android:layout_marginLeft="5dp"
            android:textSize="20sp" />
        <Button
            android:enabled="true"
            android:id="@+id/button_set_date_and_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_marginLeft="30dp"
            android:text="set_time" />
        <TextView
            android:enabled="true"
            android:id="@+id/text_date_and_time"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_marginLeft="40dp"
            android:textSize="12sp"
            android:textStyle="bold"/>


    </LinearLayout>


    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/button_confirm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:text="confirm" />
        <Button
            android:id="@+id/button_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_marginLeft="3dp"
            android:text="cancel" />

    </LinearLayout>

</RelativeLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM