LinearLayout: layout_gravity=“bottom” not working on Horizontal LinearLayout

前端 未结 7 838
青春惊慌失措
青春惊慌失措 2020-12-02 09:07

Ok, First of all, I searched all the internet, but nobody has a similar problem like this. So, all I want is to have 3 textViews, bottom aligned with the screen and with the

相关标签:
7条回答
  • 2020-12-02 09:37

    If you're ok with a RelativeLayout instead of Linear, this will do the trick, I guess:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout android:id="@+id/LinearLayout1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <TextView android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:text="@string/hello"
        android:id="@+id/TextView1" android:layout_above="@+id/TextView2"
        android:layout_alignLeft="@+id/TextView2"></TextView>
    
    <TextView android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:text="@string/hello"
        android:id="@+id/TextView2" android:layout_alignParentBottom="true">  </TextView>
    
    </RelativeLayout>
    
    0 讨论(0)
  • 2020-12-02 09:48

    If you're trying to make all three child views the same height, then change height to "0", set the android:weightSum of the LinearLayout to 3, and the set the android:layout_weight of each view to 1.

    0 讨论(0)
  • 2020-12-02 09:53

    The Correct Answer

    All the other answers are wrong. The important points:

    1. You don't need RelativeLayout. You can do this with just a LinearLayout.
    2. (Not critical but I guess you didn't know) Your weights don't need to sum to 1, you can just set them all to any equal value (e.g. 1).
    3. The critical thing is you need android:baselineAligned="false". I actually only found this by looking through the LinearLayout source. It is in the docs but they don't mention that it is on by default!

    Anyway, here is the code:

    <?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="wrap_content"
          android:baselineAligned="false">
          <TextView 
               android:text="dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg dfg"
               android:layout_weight="1"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:background="#eeffee"
               android:layout_gravity="bottom"/>
    
          <TextView 
               android:text="asd asd asd asd asd asd asd asd asd asd"
               android:layout_weight="1"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:background="#eeeeff"
               android:layout_gravity="bottom"/>
    
    
          <TextView 
               android:text="qweoiu qweoiuqwe oiqwe qwoeiu qweoiu qweoiuq weoiuqw eoiquw eoiqwue oqiweu qowieu qowieu qoiweu qowieu qowieu qowieu qowieu qoiweu qowieu qoiwue "
               android:layout_weight="1"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:background="#ffeeee"
               android:layout_gravity="bottom"/>
    
     </LinearLayout>
    

    And how it looks:

    Good linear layout

    0 讨论(0)
  • 2020-12-02 09:53

    Ok. So I had the same issue, but with toggle buttons instead of text views. For some reason, if one of the elements in the LinearLayout(Horizontal) has a different height than the rest of the views in the layout and is set to have the same gravity as the others, the gravity is effectively "ignored".

    I managed to have the desired behavior by wrapping each view inside a RelativeLayout and set the android:gravity on the relative layout instead of android:layout_gravity on each button. I also moved the android:layout_weight from the button to the relative layout.

    So instead of having:

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

    Which gives the same problem as reported, I instead did:

    <LinearLayout
      ... >
        <RelativeLayout
            ...
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="bottom" >
    
            <ToggleButton
                ...
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                />
    
        <RelativeLayout
            ...
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="bottom" >
    
            <ToggleButton
                ...
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
               />      
    
        <RelativeLayout
            ...
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="bottom" >
    
            <ToggleButton
                ...
                android:layout_height="wrap_content"
                android:layout_width="fill_parent"
                />
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-02 09:54

    **EDIT:

    If this doesn't do everything, add the baselinealigned flag as mentioned in one of the answers below by Timmmmm. That is a better way.

    Use This

    EDITED LAYOUT: Ok I edited it and also added colors and gravity to let the textviews at the bottom have equal height and width and also aligh the text at the bottom and in the center of each view.

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentBottom="true"
            android:gravity="fill_vertical" >
    
            <TextView  
                android:layout_width="match_parent" 
                android:layout_height="match_parent"
                android:layout_weight="1.0"
                android:layout_gravity="fill_vertical"
                android:gravity="bottom|center"
                android:text="text1 jkjhh  jhguk jvugy v ghjv  kjhvygvusdioc jgytuayhashg hgyff"
                android:textColor="#000000"
                android:background="#FFFF00"
                />
            <TextView  
                android:layout_width="match_parent" 
                android:layout_height="match_parent" 
                android:layout_weight="1.0"
                android:layout_gravity="fill_vertical"
                android:gravity="bottom|center"
                android:text="t2"
                android:textColor="#000000"
                android:background="#FF0000"
                />      
            <TextView  
                android:layout_width="match_parent" 
                android:layout_height="match_parent" 
                android:layout_weight="1.0"
                android:layout_gravity="fill_vertical"
                android:gravity="bottom|center"
                android:text="This is a long text. This is a long text. This is a long text. This is a long text.This is a long text. This is a long text. This is a long text."
                android:textColor="#000000"
                android:background="#00FF00"
                />
        </LinearLayout>
    

    It should do exactly what you asked.

    It uses a LinearLayout inside a RelativeLayout but sometimes it is required to nest them to get what we want. Add any more views you might want in the relative layout and you will always have your texts at the bottom as long as the last child in your RelativeLayout is this LinearLayout as shown above.

    0 讨论(0)
  • 2020-12-02 09:55

    A much easier solution would be to use the < Space > tag:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
    
          <Space
               android:layout_width="0dp"
               android:layout_height="1dp"
               android:layout_weight="1" />
    
          <TextView 
               android:text="Any Text"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:background="#eeffee"/>
    
          <TextView 
               android:text="Any Text2"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:background="#eeeeff"/>
    
    
          <TextView 
               android:text="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:background="#ffeeee"/>
    
     </LinearLayout>
    
    0 讨论(0)
提交回复
热议问题