Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Layout priority

I'm trying to divide a screen into 2 sections. The above section is a map widget. Below is an other layout filled with widgets. The problem is that the map widget fills the whole screen. How can I make the layout below the map push te map up?

I'm guessing it could be solved by giving the map widget a height, but that's not very flexible.

My attemp:

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



    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:clickable="true" 
        android:apiKey="my api key" />





    <RelativeLayout
        android:id="@+id/map_menu" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_below="@id/mapview"
        android:background="@drawable/locatie_background" >


        <Button android:id="@+id/firstButtonId" android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:text="@string/action_ready" 
            />

    </RelativeLayout >

</RelativeLayout>
like image 332
Vincent Avatar asked Mar 31 '11 11:03

Vincent


People also ask

Which layout is best for large hierarchies?

Consider using flatter layouts such as RelativeLayout or GridLayout to improve performance.

Is ConstraintLayout faster than LinearLayout?

More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout. I want to pay attention to 2 more things which are more subjective. Creating Constraint Layout takes more time than other layouts. Also introducing changes in existing Constraint Layout is much more time-consuming.

Why do we use constraint layout in Android?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time.

Which nested layout performs better with execution time?

The flatter your hierarchy, the less time that it takes for the layout stage to complete. If you are using the RelativeLayout class, you may be able to achieve the same effect, at lower cost, by using nested, unweighted LinearLayout views instead.


1 Answers

Go with a LinearLayout, set the layout_height of both children to wrap_content and set the layout_weight of both to 1.

This would divide the screens height equally:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:clickable="true"
        android:apiKey="my api key" />
    <RelativeLayout
        android:id="@+id/map_menu" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_below="@id/mapview"
        android:background="@drawable/locatie_background">
        <Button android:id="@+id/firstButtonId"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="@string/action_ready" />
    </RelativeLayout >
</LinearLayout>

You can work with layout_weight as if they would be percent values like here:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="90"
        android:clickable="true"
        android:apiKey="my api key" />
    <RelativeLayout
        android:id="@+id/map_menu" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:layout_below="@id/mapview"
        android:background="@drawable/locatie_background">
        <Button android:id="@+id/firstButtonId"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="@string/action_ready" />
    </RelativeLayout >
</LinearLayout>
like image 62
Octavian A. Damiean Avatar answered Sep 23 '22 02:09

Octavian A. Damiean