Keoki
Keoki

Reputation: 535

Android: top menu bar + scrollview?

I'm new with Android (used to develop with Visual Studio...) and so far I have a doubt with the layouts:

What I want to do is a layout with a bar at the top (just to put some button or extra information) and a scroll view that fills the rest of the screen. This is the way I am doing it so far:

<RelativeLayout 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"
tools:context=".Home" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="@android:color/white" >

    <!-- I can put any button or text I want in here -->

</LinearLayout>

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="50dp"

    android:background="@drawable/wallpaper" >


</ScrollView>

This is working as I expected, but my question is if I'm doing it the correct way or should I do it in a better way (most efficient).

Thanks in advance

Upvotes: 0

Views: 1065

Answers (1)

vortexwolf
vortexwolf

Reputation: 14037

Absolute positioning isn't recommended. For example, if you need to increase the height from 50dp to 100dp, you will have to change this value in several different places.

I know at least 2 ways to improve your layout.

1) Use features of RelativeLayout (android:layout_below="@id/linearLayout1" or its counterpart layout_above)

<RelativeLayout ...>
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/white">
    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/linearLayout1"
        android:fillViewport="true"
        android:background="@drawable/wallpaper" >
    </ScrollView>
</RelativeLayout>

2) Replace by LinearLayout (and use android:layout_weight="1.0")

<LinearLayout ...>
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/white">
    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1.0"
        android:fillViewport="true"
        android:background="@drawable/wallpaper" >
    </ScrollView>
</LinearLayout>

The line android:layout_height="0dip" may look strange. Actually you can use match_parent, but Eclipse IDE highlights such line and recommends to use 0dip if you have specified android:layout_weight.

Also I added android:fillViewport="true" to your scrollview. It indicates that the content inside the scrollview will expand to the full height if it is necessary, you can read about this property here

Upvotes: 1

Related Questions