How to fill all space in parent Relative layout?

妖精的绣舞 提交于 2019-12-25 07:32:32

问题


I created a small example to explain my problem. In the example I created a RelativeLayout that fills the whole screen. And 3 children :

  1. FrameLayout with id middle that must fill all empty space without overlay on top and bottom of FrameLayout (pink)
  2. FrameLayout with id top that must show above middle (red)
  3. FrameLayout with id bottom that must show below middle (yellow)

layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/top"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:layout_above="@id/middle"
        android:background="#FFFF0000" />

    <FrameLayout
        android:id="@+id/middle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFF00FF" />   

    <FrameLayout
        android:id="@+id/bottom"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:layout_below="@id/middle"
        android:background="#FFFFFF00" />    

</RelativeLayout>

Screenshot of my example result:

And the screenshot what I need to do:


回答1:


Okay, this code should do it.

Check it out :

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
    android:id="@+id/top"
    android:layout_width="100dp"
    android:layout_height="24dp"
    android:background="#FFFF0000"
    android:layout_alignParentTop="true" />
<FrameLayout
    android:id="@+id/bottom"
    android:layout_width="100dp"
    android:layout_height="24dp"
    android:background="#FFFFFF00"
    android:layout_alignParentBottom="true" />
<FrameLayout
    android:id="@+id/middle"
    android:layout_above="@id/bottom"
    android:layout_below="@id/top"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="#FFFF00FF" />
</RelativeLayout>



回答2:


I hope this will help you

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/top"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:background="#FFFF0000"
        android:layout_alignParentTop="true"
        />

    <FrameLayout
        android:id="@+id/middle"
        android:layout_above="@+id/bottom"
        android:layout_below="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFF00FF" />
    <FrameLayout
        android:id="@+id/bottom"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:background="#FFFFFF00"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>



回答3:


Something like this should work:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <FrameLayout
        android:id="@+id/middle"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="24dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFF00FF" />

    <FrameLayout
        android:id="@+id/top"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:background="#FFFF0000" />

    <FrameLayout
        android:id="@+id/bottom"
        android:layout_width="100dp"
        android:layout_height="24dp"
        android:layout_gravity="bottom"
        android:background="#FFFFFF00" />

</FrameLayout>



回答4:


Instead of using RelativeLayout you can use LinearLayout and make use of layout_weight to fill the remaining space.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<FrameLayout
    android:id="@+id/top"
    android:layout_width="100dp"
    android:layout_height="24dp"
    android:background="#FFFF0000" />

<FrameLayout
    android:id="@+id/middle"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:background="#FFFF00FF"
    android:layout_weight="1"/>

<FrameLayout
    android:id="@+id/bottom"
    android:layout_width="100dp"
    android:layout_height="24dp"
    android:background="#FFFFFF00" />

</LinearLayout>



回答5:


To do this with relative layout you need to align top and bottom layouts to borders, and than set the middle layout in relation between them.

See below:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="pl.orbitemobile.myapplication.MainActivity">


            <FrameLayout
                android:id="@+id/top"
                android:layout_width="100dp"
                android:layout_height="24dp"
                android:layout_alignParentTop="true"
                android:background="#FFFF0000" />

            <FrameLayout
                android:id="@+id/middle"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:layout_below="@id/top"
                android:layout_above="@+id/bottom"
                android:background="#FFFF00FF" />

            <FrameLayout
                android:layout_width="100dp"
                android:id="@+id/bottom"
                android:layout_height="24dp"
                android:layout_alignParentBottom="true"
                android:background="#FFFFFF00" />

</RelativeLayout>

Effect:



来源:https://stackoverflow.com/questions/40935591/how-to-fill-all-space-in-parent-relative-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!