如何在Android线性布局中将父元素底部对齐?

12

我有一个线性布局

我想在它的底部创建一个片段。

我知道有一些选项,但我有点困惑

1)android:layout_gravity:"bottom" --> 对我来说无效。

2)android:gravity_weight="0" 并给它前面的兄弟 android:gravity_weight:"1"

3)android:height="wrap_content" 并给它前面的兄弟 android:height:"match_parent"

我知道如何使用RelativeLayout实现这一点,但我想练习LinearLayout

你会建议什么?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue_bg"
    android:orientation="vertical" >

   <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/signup_illu_why" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=""
        android:orientation="horizontal" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/signup_skip_icon" />

</LinearLayout>

3
你尝试过使用RelativeLayout吗?它可能更适合你的需求,而且RelativeLayout通常更高效、更推荐使用。 - janos
我知道如何使用RelativeLayout来实现这个,但我想练习LinearLayout。 - Elad Benda
类似的解决方案 - 请查看此答案:https://dev59.com/6XfZa4cB1Zd3GeqPOzdd#31987803 - ashumeow
7个回答

20

实际上,您可以将父元素的重力设置为底部。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue_bg"
    android:orientation="vertical"
    android:gravity="bottom" >

   <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/signup_illu_why" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=""
        android:orientation="horizontal" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/signup_skip_icon" />

</LinearLayout>

4
但是接下来一切都将到达底部。 - user25
2
这是正确的答案 - https://dev59.com/IGIj5IYBdhLWcg3wGRmM#20633003 我们应该使用RelativeLayout作为父布局,并为我们想要放在底部的子布局设置android:layout_alignParentBottom="true" - user25
@user25 这个问题没有指定哪个子视图应该底部对齐,所以我假设他希望每个子视图都在底部。 - Rick Royd Aban

12
<LinearLayout
    android:orientation="vertical"
                              ... >

    <Space
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <SomeViewThatNeedsGoBottom
                             ... />

</LinearLayout>

6

试一试

  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="#ffffff"
        android:orientation="vertical" >

       <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:src="@drawable/ic_launcher" />
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
             android:layout_alignParentBottom="true"
            android:orientation="vertical" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

    </LinearLayout>

    </RelativeLayout>

2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:orientation="vertical" 
    android:weightSum="1">

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

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="0.2" />

</LinearLayout>

你不能将 android:orientation 添加到 RelativeLayout。 - Batuhan Coşkun
是的,请检查已编辑的答案。那是因为我直接更改了布局,而不是手动修改。 - Vaibhav Agarwal
我知道如何使用RelativeLayout来实现这个,但我想练习LinearLayout。 - Elad Benda
请检查我编辑后的答案,使用线性布局即可实现。虽然两种布局都可以实现,但如果您使用相对布局会更好,因为它更简单易用。请根据需要更改图像名称和颜色。 - Vaibhav Agarwal

0

使用:

android:gravity="bottom"

例子:

<LinearLayout
  android:layout_weight="7"
  android:layout_width="0dp"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:weightSum="5">
  <TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="some text"
     android:layout_weight="2"
     android:gravity="bottom" />
  <TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="some text"
     android:layout_weight="1" />
  <TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="some text"android:gravity="center"
     android:layout_weight="2" />
</LinearLayout>

0

将你的线性布局用帧布局包围起来,并将重心设置为底部。 比你想象的简单...许多布局只是为了更轻松地移动设计而使用。

<FrameLayout
android:layout_height="fill_parent"
android:layout_width = "fill_parent">

<LinearLayout`
android:layout_height="wrap_content"
android:layout_width = "wrap_content"
android:layout_gravity = "bottom"
android:orientation = "vertical">

<Button
android:layout_height="wrap_content"
android:layout_width = "wrap_content"
android:text = "btn"/>

</LinearLayout>

</FrameLayout>

`


0
将相对布局(Relative)放入线性布局(LinearLayout)中,并将相对布局的属性设置为matchparent,这样就可以使用layoutalignparentbottom属性。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue_bg"
    android:orientation="vertical" >

   <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/signup_illu_why" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight=""
        android:orientation="horizontal" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/signup_skip_icon"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>

</LinearLayout>

为您的答案添加解释会很有帮助! - abhiarora
虽然这段代码可能提供了问题的解决方案,但最好添加上为什么/如何运作的上下文。这可以帮助未来的用户学习,并将这些知识应用到他们自己的代码中。当代码被解释时,您还可能会得到用户的积极反馈,例如点赞。 - borchvm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接