RelativeLayout is taking fullscreen for wrap_content

前端 未结 6 869
闹比i
闹比i 2020-11-27 14:58

Why does FOOBARZ get layed out all the way at the bottom when no elements are layout_height=\"fill_parent\" in other words, all elements are wrap_content for he

相关标签:
6条回答
  • 2020-11-27 15:29

    For those looking for a solution to this, like I did, you can use FrameLayout instead of RelativeLayout.

    Then you can set the gravity the intended object to bottom right as below

    <TextView
        android:layout_gravity="bottom|right"
        android:text="FOOBARZ"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
    </TextView>
    
    0 讨论(0)
  • 2020-11-27 15:35

    Dont use alight_Parent type properties with the child views

    You can use frame layout instead of RelativeLayout with respective gravity

        <FrameLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
         <TextView
            android:layout_gravity="bottom|right"
            android:text="Hello "
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
    
        </TextView>
    
    </FrameLayout>
    
    0 讨论(0)
  • 2020-11-27 15:42

    Good answers. Now if you don't have layout_alignParentBottom="true" and still getting this issue watch out for android:background="@drawable/bkgnd" where bkgnd is a biggie.

    0 讨论(0)
  • 2020-11-27 15:46

    You have set the RelativeLayout to "wrap_content" and the TextView to android:layout_alignParentBottom="true", so it automatically tries to stretch the RelativeLayout to the bottom. Don't use such dependencies with Relative Layout, as it can count as "circular dependencies".

    From the docs for RelativeLayout:

    Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM.

    Try to align your TextView to something other than the parent RelativeLayout, but watch out for this problem as well:
    Circular dependencies, need some help with exact code

    Alternatively, try to add more sophisticated inner layouts.

    0 讨论(0)
  • 2020-11-27 15:47

    From the RelativeLayout doc:

    Class Overview

    A Layout where the positions of the children can be described in relation to each other or to the parent.

    Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM

    Class documentation

    Which is exactly your case. RelativeLayout can not do that.

    0 讨论(0)
  • 2020-11-27 15:53

    I'm not sure why the clean and obvious way of accomplishing this hasn't been posted yet. This performant solution works for any View MyView with a known height.

    Wrap your RelativeLayout with height wrap_content in a FrameLayout:

    <!-- width here should constrain RelativeLayout -->
    <FrameLayout 
         android:layout_width="@dimen/my_layout_width"
         android:layout_height="wrap_content">
    
         <RelativeLayout  
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />
    
         <MyView
            ...
            android:layout_gravity="bottom" />
    </FrameLayout>
    

    Just note that the view at the bottom of the FrameLayout will be on top of your RelativeLayout content, so you'll need to add padding to the bottom of that layout to accomodate it. If you want that view to be variable height, you can either Subclass FrameLayout to add padding in code based on the measured view height, or just change the FrameLayout to vertical LinearLayout if you're not worried about the performance, i.e. it's not a listview item, or the views are relatively lightweight.

    0 讨论(0)
提交回复
热议问题