Android Linear Layout - How to Keep Element At Bottom Of View?

前端 未结 8 1489
悲哀的现实
悲哀的现实 2020-12-08 01:50

I have a TextView which I want to pin at the bottom of a landscape activity that is using LinearLayout with vertically arranged elements.

相关标签:
8条回答
  • 2020-12-08 02:04

    try this

    <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>
    
    0 讨论(0)
  • 2020-12-08 02:06

    I think it will be perfect solution:

    <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>
    
    0 讨论(0)
  • 2020-12-08 02:06

    Step 1 : Create two view inside a linear layout

    Step 2 : First view must set to android:layout_weight="1"

    Step 3 : Second view will automatically putted downwards

    <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>
    
    0 讨论(0)
  • 2020-12-08 02:09

    Update: I still get upvotes on this question, which is still the accepted answer and which I think I answered poorly. In the spirit of making sure the best info is out there, I have decided to update this answer.

    In modern Android I would use ConstraintLayout to do this. It is more performant and straightforward.

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

    If you don't want to use a ConstraintLayout, using a LinearLayout with an expanding view is a straightforward and great way to handle taking up the extra space (see the answer by @Matthew Wills). If you don't want to expand the background of any of the Views above the bottom view, you can add an invisible View to take up the space.

    The answer I originally gave works but is inefficient. Inefficiency may not be a big deal for a single top level layout, but it would be a terrible implementation in a ListView or RecyclerView, and there just isn't any reason to do it since there are better ways to do it that are roughly the same level of effort and complexity if not simpler.

    Take the TextView out of the LinearLayout, then put the LinearLayout and the TextView inside a RelativeLayout. Add the attribute android:layout_alignParentBottom="true" to the TextView. With all the namespace and other attributes except for the above attribute elided:

    <RelativeLayout>
      <LinearLayout>
        <!-- All your other elements in here -->
      </LinearLayout>
      <TextView
        android:layout_alignParentBottom="true" />
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-08 02:13

    You will have to expand one of your upper views to fill the remaining space by setting android:layout_weight="1" on it. This will push your last view down to the bottom.

    Here is a brief sketch of what I mean:

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

    where each of the child view heights is "wrap_content" and everything else is "fill_parent".

    0 讨论(0)
  • 2020-12-08 02:19

    You should put the parameter gravity to bottom not in the textview but in the Linear Layout. Like this:

    <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>
    
    0 讨论(0)
提交回复
热议问题