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

And*_*der 71 android android-layout android-linearlayout

我有一个TextView我想要在LinearLayout垂直排列元素使用的景观活动的底部固定.

我已经设置android:gravity="bottom"了文本视图,但它仍然喜欢在LinearLayout我不想要它的最后一个元素之下.

有什么建议?

Mat*_*lis 113

您必须展开其中一个上部视图以通过设置来填充剩余空间android:layout_weight="1".这会将您的上一个视图推到底部.

这是我的意思的简要草图:

<LinearLayout android:orientation="vertical">
    <View/>
    <View android:layout_weight="1"/>
    <View/>
    <View android:id="@+id/bottom"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

每个儿童视野的高度"wrap_content"和其他一切都在哪里"fill_parent".

  • 你必须添加宽度和高度:<查看android:layout_weight ="1"android:layout_height ="wrap_content"android:layout_width ="wrap_content"/> (3认同)
  • 这真太了不起了.我总是忘记这个伎俩,并最终花费我的布局.这太棒了.解决世界饥饿问题. (2认同)

Bri*_*ley 101

更新:我仍然对这个问题有所了解,这仍然是公认的答案,我认为我的回答很差.本着确保最佳信息的精神,我决定更新这个答案.

在现代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" />
Run Code Online (Sandbox Code Playgroud)

如果您不想使用ConstraintLayout,则使用具有展开视图的LinearLayout是一种直接且很好的方式来处理占用额外空间(请参阅@Matthew Wills的答案).如果您不想展开底部视图上方任何视图的背景,则可以添加不可见的视图以占用空间.

我最初给出的答案有效,但效率低下.对于单个顶级布局而言,低效率可能不是一个大问题,但它在一个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>
Run Code Online (Sandbox Code Playgroud)

  • 对于类似于布局为ListView充气100或1000倍的东西,绝对是.对于Activity的单一布局,它并不是很受欢迎.你正在为程序员提供便利,以获得微小的性能. (3认同)

Svi*_*sev 53

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

<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>
Run Code Online (Sandbox Code Playgroud)

  • 你是个天才!!我为此浪费了 2 天,但在那里错过了一些东西,谢谢(不使用相对和组件是查看我的布局的寻呼机,因此它的创建问题)--我想给出不止一点,但不能 (2认同)

Pau*_*nez 10

您应该将参数gravity置于底部,而不是在textview中,而是放在Linear Layout中.像这样:

<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>
Run Code Online (Sandbox Code Playgroud)


Vin*_*shi 7

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

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

第3步:第二个视图将自动向下推

<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>
Run Code Online (Sandbox Code Playgroud)