简体   繁体   English

使用LinearLayout将按钮对齐到底部

[英]Align Button to Bottom using LinearLayout

Trying to align button at Bottom using LinearLayout, but getting just below TextView. 尝试使用LinearLayout在底部对齐按钮,但正好位于TextView下方。

To set button at bottom, I am using android:layout_gravity="bottom" but still not done 要将按钮设置在底部,我正在使用android:layout_gravity="bottom"但仍未完成

LinearLayout xml LinearLayout XML

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dp"
        android:layout_height="match_parent"            
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_gravity="bottom"
            android:layout_height="wrap_content">

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Bottom" />

        </LinearLayout>

 </LinearLayout>

Change second linear layout to 将第二个线性布局更改为

<LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal" >

This will put the button at the bottom and this layout will take the rest of the space 这会将按钮置于底部,此布局将占用其余空间

Yoy have to use like this.... Yoy必须这样使用...。

<RelativeLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center">
        <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Bottom" />
    </RelativeLayout>
  <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="0dp"
        android:layout_height="match_parent"            
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"/>

        </LinearLayout>

        <LinearLayout
             android:layout_weight="1"
            android:layout_width="fill_parent"
            android:layout_gravity="bottom"
            android:layout_height="wrap_content">

            <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="Bottom" />

        </LinearLayout>

 </LinearLayout>

I could not use the suggested answer as I had multiples of 3 vertical ones (each with specific weights) inside a horizontal one. 我无法使用建议的答案,因为我在一个水平答案中有3个垂直答案的倍数(每个都有特定的权重)。 So, ended up using margin top for specific button/widget instead. 因此,最终将margin top用于特定的按钮/小部件。 Working fine so far. 到目前为止工作正常。

Did not want to change to RelativeLayout as that meant many changes in this scenario. 不想更改为RelativeLayout,因为这意味着此方案中有许多更改。

示例布局

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

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