Android 线性布局 - 如何将元素保持在视图底部?

问题描述 投票:0回答:9

我有一个

TextView
,我想将其固定在使用
LinearLayout
和垂直排列元素的景观活动的底部。

我已经在文本视图上设置了

android:gravity="bottom"
,但它仍然喜欢位于
LinearLayout
的最后一个元素之下,这正是我不希望它做的。

有什么建议吗?

android android-layout android-linearlayout
9个回答
130
投票

您必须通过在其上设置

android:layout_weight="1"
来扩展您的上视图之一以填充剩余空间。这会将您的最后一个视图推到底部。

这里是我的意思的简要说明:

<LinearLayout android:orientation="vertical">
    <View/>
    <View android:layout_weight="1"/>
    <View/>
    <View android:id="@+id/bottom"/>
</LinearLayout>

每个孩子的视图高度都是

"wrap_content"
,其他一切都是
"fill_parent"
.


110
投票

更新:我仍然对这个问题投赞成票,这仍然是公认的答案,我认为我回答得不好。本着确保提供最佳信息的精神,我决定更新此答案。

在现代 Android 中,我会使用

ConstraintLayout
来做到这一点。它更高效、更直接。

<ConstraintLayout>
   <View
      android:id="@+id/view1"
      ...other attributes elided... />
   <View
      android:id="@id/view2"        
      app:layout_constraintTop_toBottomOf="@id/view1" />
      ...other attributes elided... />

   ...etc for other views that should be aligned top to bottom...

   <TextView
    app:layout_constraintBottom_toBottomOf="parent" />

如果您不想使用 ConstraintLayout,使用带有扩展视图的 LinearLayout 是处理占用额外空间的一种直接且很好的方法(请参阅@Matthew Wills 的回答)。如果不想在bottom view上方扩展任何View的背景,可以添加一个不可见的View来占用空间。

我最初给出的答案有效但效率低下。对于单个顶层布局来说,效率低下可能不是什么大问题,但在

ListView
RecyclerView
中,这将是一个糟糕的实现,而且没有任何理由这样做,因为有更好的方法来做到这一点如果不是更简单的话,它们的工作量和复杂性大致相同。

将TextView从LinearLayout中取出,然后将LinearLayout和TextView放在一个RelativeLayout中。将属性

android:layout_alignParentBottom="true"
添加到 TextView。除了上述属性之外的所有名称空间和其他属性都被省略:

<RelativeLayout>
  <LinearLayout>
    <!-- All your other elements in here -->
  </LinearLayout>
  <TextView
    android:layout_alignParentBottom="true" />
</RelativeLayout>

65
投票

我认为这将是完美的解决方案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Other views -->
    <Space
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <!-- Target view below -->
    <View
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

</LinearLayout>

12
投票

您应该将参数gravity 放在底部,而不是在文本视图中,而在线性布局 中。像这样:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="bottom|end">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Something"/>
</LinearLayout>

12
投票

第 1 步:在线性布局中创建两个视图

第 2 步:第一个视图必须设置为 android:layout_weight="1"

第三步:第二个视图会自动向下放置

<LinearLayout
android:id="@+id/botton_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

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

    <Button
    android:id="@+id/btn_health_advice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>


</LinearLayout>

1
投票

您也可以使用

android:layout_gravity="bottom"

为您的文本视图


1
投票

喜欢这个

 <LinearLayout
android:id="@+id/LinearLayouts02"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="bottom|end">

<TextView
    android:id="@+id/texts1"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:layout_weight="2"
    android:text="@string/forgotpass"
    android:padding="7dp"
    android:gravity="bottom|center_horizontal"
    android:paddingLeft="10dp"
    android:layout_marginBottom="30dp"
    android:bottomLeftRadius="10dp"
    android:bottomRightRadius="50dp"
    android:fontFamily="sans-serif-condensed"
    android:textColor="@color/colorAccent"
    android:textStyle="bold"
    android:textSize="16sp"
    android:topLeftRadius="10dp"
    android:topRightRadius="10dp"
   />

</LinearLayout>

0
投票

只需在 LinearLayout 中使用

android:gravity="bottom"


-2
投票

试试这个

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

    <TextView
        android:id="@+id/textViewProfileName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>
© www.soinside.com 2019 - 2024. All rights reserved.