Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fill space between two views within a RelativeLayout

I have two views. The top view is set to ...

 android:layout_alignParentTop="true"

And the bottom is set to ...

 android:layout_alignParentBottom="true"

How can I fill the remaining space with a third view? According to this answer here, I should use a frame layout like this ...

<FrameLayout 
   android:layout_below="@+id/toplayout" 
   android:layout_above="@+id/bottomlayout"/>

But then I am required to specify height and width. What height and width am I supposed to specify?

like image 926
the_prole Avatar asked Nov 17 '15 01:11

the_prole


People also ask

How can I add space between two views in android?

Use the setPadding(left, top, right, bottom) method on the view to set the padding. The integers passed as parameters to this function are the number of pixels (px), which are different from the density-independent pixels (dp or dip).

How views are aligned inside a RelativeLayout?

RelativeLayout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).

Can we use RelativeLayout inside ConstraintLayout?

You can't use relative layout directly inside constraint layout. Intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.

When to use Relative layout?

Basically, it is used to display more views neatly on the screen. A RelativeLayout is a common type of ViewGroup that lets us position child views relative to sibling elements (such as to the left-of or below another view) or relative to the parent RelativeLayout area (such as aligned to the bottom, left or center).


1 Answers

Here is my solution

<RelativeLayout
     ...
   >
        <YourLayout
         android:id="@+id/toplayout"
         android:layout_alignParentTop="true"
        />

        <YourLayout
         android:id="@+id/bottomlayout"
         android:layout_alignParentBottom="true"
        />

        <MiddleLayout 
          <!-- in your case it is FrameLayout -->
           android:layout_width="match_parent"
           android:layout_height="match_parent" 
          <!-- or android:layout_height="wrap_content" according to the_profile -->
           android:layout_below="@+id/toplayout" 
           android:layout_above="@+id/bottomlayout"/>
    </RelativeLayout>

Hope this help

like image 185
Linh Avatar answered Oct 21 '22 01:10

Linh