1

Estou trabalhando em um projeto onde tenho uma tela que contém alguns elementos separados por tipos de layout diferentes. Todos esses elementos precisaram ser agrupados dentro de um único LinearLayout para que eu pudesse "envelopá-los" em um ScrollView. O problema é que um desses layouts está simplesmente 'jogando' para fora de vista outro, ocupando todo o espaço disponível. O resultado é esse:

Erro do além

Perceba na parte em azul que tem um layout (no caso, um RelativeLayout) esmagado para fora da tela. Esse layout deveria estar fixo na parte de baixo da tela, porém se eu mudar o tipo do preenchimento do LinearLayout imediatamente acima dele, esse é o resultado:

Tá certo mas tá errado

Os botões CANCELAR e SALVAR deveriam ficar fixos na parte de baixo da tela. Esse é o XML usado no layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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/content_new_color"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clipToPadding="false"
    android:fillViewport="true"
    android:scrollbarStyle="insideOverlay"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ions.colorcodes.activities.NewColorActivity"
    tools:showIn="@layout/activity_new_color">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/input_color_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/hint_color_name" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/input_layout_code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/input_color_code"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/hint_color_code" />

            </android.support.design.widget.TextInputLayout>

            <android.support.v4.widget.Space
                android:layout_width="match_parent"
                android:layout_height="16dp" />

            <TextView
                android:id="@+id/info_preview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/info_preview" />

            <android.support.v4.widget.Space
                android:layout_width="match_parent"
                android:layout_height="8dp" />

            <LinearLayout
                android:id="@+id/card_preview_wrapper"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:weightSum="1">

                <android.support.v7.widget.CardView
                    android:id="@+id/card_preview"
                    android:layout_width="160dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_margin="@dimen/card_margin"
                    android:elevation="4dp"
                    app:cardCornerRadius="@dimen/card_album_radius">

                    <RelativeLayout
                        android:id="@+id/preview_card"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                        <TextView
                            android:id="@+id/new_color_name"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentStart="true"
                            android:layout_below="@+id/new_color_preview"
                            android:paddingLeft="@dimen/album_title_padding"
                            android:paddingRight="@dimen/album_title_padding"
                            android:paddingTop="@dimen/album_title_padding"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/new_color_hex_code"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_alignParentStart="true"
                            android:layout_below="@+id/new_color_name"
                            android:paddingBottom="@dimen/songs_count_padding_bottom"
                            android:paddingLeft="@dimen/album_title_padding"
                            android:paddingRight="@dimen/album_title_padding" />

                        <ImageView
                            android:id="@+id/new_color_preview"
                            android:layout_width="match_parent"
                            android:layout_height="128dp"
                            android:layout_alignParentStart="true"
                            android:layout_alignParentTop="true"
                            android:background="#ffffff"
                            android:clickable="true"
                            android:scaleType="fitXY" />

                    </RelativeLayout>
                </android.support.v7.widget.CardView>
            </LinearLayout>
        </LinearLayout>

        <RelativeLayout
            android:id="@+id/preview_button_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="bottom"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/btn_cancel_new_color"
                    style="@style/Widget.AppCompat.Button.Borderless"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/btn_cancel" />

                <Button
                    android:id="@+id/btn_save_new_color"
                    style="@style/Widget.AppCompat.Button.Borderless.Colored"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="@string/btn_save" />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

Onde eu estou errando? Estou procurando soluções por um bom tempo e não consigo encontrar nada que me ajude.

2 Respostas 2

1

Tente deixar o layout dos buttons fora do ScrollView +- assim:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"         
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_segunda"
        tools:context="br.myapplication.SegundaActivity"
        android:orientation="vertical"
        android:weightSum="1">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="416dp"
    android:id="@+id/scrollView">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/input_color_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Nome da Cor" />

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_code"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/input_color_code"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Código da Cor" />

        </android.support.design.widget.TextInputLayout>

        <android.support.v4.widget.Space
            android:layout_width="match_parent"
            android:layout_height="16dp" />

        <TextView
            android:id="@+id/info_preview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Preview" />

        <android.support.v4.widget.Space
            android:layout_width="match_parent"
            android:layout_height="8dp" />

        <RelativeLayout
            android:id="@+id/card_preview_wrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1">

            <android.support.v7.widget.CardView
                android:id="@+id/card_preview"
                android:layout_width="160dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:elevation="4dp"
                app:cardCornerRadius="5dp"
                android:layout_centerHorizontal="true">

                <RelativeLayout
                    android:id="@+id/preview_card"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/new_color_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:layout_below="@+id/new_color_preview"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp"
                        android:paddingTop="10dp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/new_color_hex_code"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:layout_below="@+id/new_color_name"
                        android:paddingBottom="10dp"
                        android:paddingLeft="10dp"
                        android:paddingRight="10dp" />

                    <ImageView
                        android:id="@+id/new_color_preview"
                        android:layout_width="match_parent"
                        android:layout_height="128dp"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentTop="true"
                        android:background="#ffffff"
                        android:clickable="true"
                        android:scaleType="fitXY" />

                </RelativeLayout>
            </android.support.v7.widget.CardView>
        </RelativeLayout>
    </LinearLayout>
</ScrollView>

<LinearLayout
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:orientation="horizontal"
   android:layout_weight="0.51">
    <Button
        android:id="@+id/btn_cancel_new_color"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/btn_cancel" />

    <Button
        android:id="@+id/btn_save_new_color"
        style="@style/Widget.AppCompat.Button.Borderless.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/btn_save" />
</LinearLayout>
</LinearLayout>
1

Segundo o que eu percebi, você quer colocar os botões fixos no fundo do ecrã e a scrollview preencher o espaço restante.

Para tal pode fazer do seguinte modo

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/content_new_color"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:clipToPadding="false"
        android:fillViewport="true"
        android:scrollbarStyle="insideOverlay"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.ions.colorcodes.activities.NewColorActivity"
        tools:showIn="@layout/activity_new_color">

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_layout_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/input_color_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/hint_color_name" />

                </android.support.design.widget.TextInputLayout>

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/input_layout_code"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <EditText
                        android:id="@+id/input_color_code"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/hint_color_code" />

                </android.support.design.widget.TextInputLayout>

                <android.support.v4.widget.Space
                    android:layout_width="match_parent"
                    android:layout_height="16dp" />

                <TextView
                    android:id="@+id/info_preview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/info_preview" />

                <android.support.v4.widget.Space
                    android:layout_width="match_parent"
                    android:layout_height="8dp" />

                <LinearLayout
                    android:id="@+id/card_preview_wrapper"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:weightSum="1">

                    <android.support.v7.widget.CardView
                        android:id="@+id/card_preview"
                        android:layout_width="160dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_margin="@dimen/card_margin"
                        android:elevation="4dp"
                        app:cardCornerRadius="@dimen/card_album_radius">

                        <RelativeLayout
                            android:id="@+id/preview_card"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent">

                            <TextView
                                android:id="@+id/new_color_name"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentStart="true"
                                android:layout_below="@+id/new_color_preview"
                                android:paddingLeft="@dimen/album_title_padding"
                                android:paddingRight="@dimen/album_title_padding"
                                android:paddingTop="@dimen/album_title_padding"
                                android:textStyle="bold" />

                            <TextView
                                android:id="@+id/new_color_hex_code"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_alignParentStart="true"
                                android:layout_below="@+id/new_color_name"
                                android:paddingBottom="@dimen/songs_count_padding_bottom"
                                android:paddingLeft="@dimen/album_title_padding"
                                android:paddingRight="@dimen/album_title_padding" />

                            <ImageView
                                android:id="@+id/new_color_preview"
                                android:layout_width="match_parent"
                                android:layout_height="128dp"
                                android:layout_alignParentStart="true"
                                android:layout_alignParentTop="true"
                                android:background="#ffffff"
                                android:clickable="true"
                                android:scaleType="fitXY" />

                        </RelativeLayout>
                    </android.support.v7.widget.CardView>
                </LinearLayout>
            </LinearLayout>


        </LinearLayout>
    </ScrollView>

    <RelativeLayout
        android:id="@+id/preview_button_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="bottom"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btn_cancel_new_color"
                style="@style/Widget.AppCompat.Button.Borderless"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/btn_cancel" />

            <Button
                android:id="@+id/btn_save_new_color"
                style="@style/Widget.AppCompat.Button.Borderless.Colored"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/btn_save" />
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .