How to place a button with static position on the scrollview

Asked

Viewed 389 times

1

Greetings dear Developers. I have an Empty Activity with the entire screen occupied by a scrollview containing only imageviews and I need it to have at the top of the screen a button with a fixed position regardless of scroll, and to open a menu listed

  • From what I understand, just leave this button out of the scroll

  • Thanks Murillo, really it was a breakthrough for me but I wish that button is visible above the images that are in scrollview

1 answer

1


I’ll leave 2 methods here

The first using Layout with Linearlayout:

<?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"
    android:padding="20dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Button1" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="20dp">

            <!--Aqui você coloca suas imagens-->

        </LinearLayout>
    </ScrollView>
</LinearLayout>

Resultado 1

The second using Constraint Layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="@color/colorAccent"
        android:text="Button1"
        app:layout_constraintLeft_toLeftOf="@+id/activity_main"
        app:layout_constraintRight_toRightOf="@+id/activity_main"
        app:layout_constraintTop_toTopOf="@+id/activity_main" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="20dp">

            <!-- Aqui você coloca suas imagens-->

        </LinearLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

Resultado2

  • 1

    worked perfectly Murillo, my thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.