Search code examples
androidandroid-layoutandroid-scrollviewxml-layout

I need to use scrollview correctly


I have 3 buttons and scroll view contains text and images. I want the scrollview effect only for the images and texts and my 3 buttons fixed over the scrollview how can i do that ?


Solution

  • Use a FrameLayout.

    An example of a top bar with three buttons over a ScrollView with images is demonstrated below.

    FrameLayout's render their children in the order they are declared. For the example below, the ScrollView is rendered first, and the LinearLayout button bar is rendered last. This places the bar above the scrollview and gives the desired effect.

    The reason the ScrollView has a LinearLayout as a child, is because they can only have one child.

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
        </ScrollView>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal">
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
    
            <Button
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"/>
        </LinearLayout>
    </FrameLayout>