Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android layout: place a view between two views

I am trying to make a screen, preferably with XML only, to look like this: enter image description here

I followed this approach, but using FrameLayout I only have the option to position the "orange" view using layout_gravity, and as I try to explain in the image, I don't know the layouts' height, because both measure with layout_weight.

My layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="onit.radiolisten.MainActivity">

    <FrameLayout
        android:id="@+id/layout_container_activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <!-- TOP LAYOUT, THE GREEN ONE ON THE IMAGE -->
            <LinearLayout
                android:id="@+id/layout_top_activity_main"
                android:layout_width="match_parent"
                android:layout_height="0dip"
                android:layout_weight="2"
                android:background="@color/colorPrimary"
                android:orientation="vertical"></LinearLayout>

            <!-- BOTTOM LAYOUT, THE BLUE ONE ON THE IMAGE -->
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="#e6e6e6">

            </RelativeLayout>
        </LinearLayout>

        <!-- THIS IS THE IMAGE I WANT TO POSITION -->
        <ImageView
            android:id="@+id/btn_play_radio"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_gravity="center"
            android:src="@drawable/play_icon" />
    </FrameLayout>

</LinearLayout>

Can this be done with XML only?

like image 504
RominaV Avatar asked May 06 '16 19:05

RominaV


People also ask

What is nested view Android?

By the term of Nested we mean one Layout inside of other Layout. In Android all layout can be nested one another. In this example we create a Registration Form with multiple fields using Nested Linear Layouts.

How to merge two layouts in Android?

In Android, for reusing the complete layouts we can use the <include/> and <merge/> tags to embed another layout inside the current layout. Android offers a variety of widgets to provide small and reusable interactive elements. In some cases we might also need to reuse larger components that require a special layout.

What is a Relative view?

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).


1 Answers

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<--main layout-->      
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
<--top layout-->
    <LinearLayout    
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="20dp"
        android:layout_weight="0.4"
        android:gravity="bottom|center"/>

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.1"
        android:background="@color/callingscreen_color"
        android:gravity="center|top" />     
    </LinearLayout>
<--layout for img-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1.5" >
      </LinearLayout>

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="0.5" 
          android:gravity="center|top">
<--IMage u want to put-->
          <ImageView
              android:id="@+id/ivimg"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:src="@drawable/img"/>
      </LinearLayout>
    </LinearLayout>
 </RelativeLayout>
like image 75
Nikhil Avatar answered Nov 04 '22 12:11

Nikhil