Layout Hierarchy

Asked

Viewed 256 times

0

I want my Toolbar I put in place of Actionbar not inside Scroolview, but always when I take it and create a Relativelayout or Linearlayout on the outside and I put the Toolbar and then the Scroolview it does not appear.

This is my current XML:

<RelativeLayout 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">


    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView"
        style="@style/FundoAPP"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right">


        <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="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:weightSum="1"
            tools:context="com.example.joel.housepharm.ClasseTelas.InsProgramacao">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbarSessao"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize">

            </android.support.v7.widget.Toolbar>
...
            </RelativeLayout>


            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp">

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

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <ListView
                            android:id="@+id/JN_04_06"
                            android:layout_width="210dp"
                            android:layout_height="190dp"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentStart="true"
                            android:layout_centerVertical="true"
                            android:divider="@color/colorAccentSecurdary" />

                        <ListView
                            android:id="@+id/JN_04_07"
                            android:layout_width="140dp"
                            android:layout_height="190dp"
                            android:layout_alignParentEnd="true"
                            android:divider="@color/colorAccentSecurdary" />
                    </RelativeLayout>
                </ScrollView>
            </android.support.design.widget.TextInputLayout>
            <!--style="@style/CamposImput"-->


            <!--
            <RelativeLayout
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:gravity="center_horizontal">
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/cancelar"
                    android:id="@+id/JN_04_11"
                    android:onClick="JN_04_Fechar"
                    style="@style/Botoes"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/confirma"
                    android:id="@+id/JN_04_12"
                    android:onClick="clickBtnProg"

                    style="@style/Botoes"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true" />

            </RelativeLayout>
            -->
            <View style="@style/Divider" />

            <TextView
                style="@style/Textos"
                android:layout_width="match_parent"

                android:layout_height="match_parent"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp"
                android:text="@string/Repet" />

            <RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp">

                <CheckBox
                    android:id="@+id/chkUmaVez"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="@string/somenteUma" />

                <CheckBox
                    android:id="@+id/chkTodosDias"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="false"
                    android:text="@string/todosDias" />

                <RelativeLayout
                    android:layout_width="193dp"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/textView"
                        style="@style/Textos"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:layout_centerVertical="true"
                        android:text="@string/Repetir"
                        android:textAppearance="?android:attr/textAppearanceMedium" />

                    <EditText
                        android:id="@+id/horasRep"
                        android:layout_width="30dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_toEndOf="@+id/textView"
                        android:ems="2"
                        android:inputType="number"
                        android:maxLength="2"
                        android:maxLines="1"
                        android:textAlignment="center" />

                    <TextView
                        android:id="@+id/textView22"
                        style="@style/Textos"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/textView"
                        android:layout_toEndOf="@+id/horasRep"
                        android:text="@string/horas"
                        android:textAppearance="?android:attr/textAppearanceMedium" />
                </RelativeLayout>
            </RadioGroup>

            <TextView
                android:id="@+id/textView12"
                style="@style/Textos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp"
                android:text="@string/CampoObrigatorio"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/colorAccent" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

This is the new, that the Toolbar does not appear:

<RelativeLayout 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.support.v7.widget.Toolbar
        android:id="@+id/toolbarSessao"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize">

    </android.support.v7.widget.Toolbar>

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView"
        style="@style/FundoAPP"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="right">


        <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="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:weightSum="1"
            tools:context="com.example.joel.housepharm.ClasseTelas.InsProgramacao">



            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp">

                <EditText
                    android:id="@+id/JN_04_01"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:hint="@string/hintHora"
                    android:textColorHint="#ffa3a3a3" />


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

            <TextView
                android:id="@+id/textView2"
                style="@style/Textos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp"
                android:text="@string/RemSess"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <fr.ganfra.materialspinner.MaterialSpinner
                android:id="@+id/JN_04_02"
                style="@style/SinnerMaterialDesign"
                android:layout_width="340dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                app:ms_enableErrorLabel="false"
                app:ms_enableFloatingLabel="false"
                app:ms_multiline="false"
                app:ms_thickness="1dp">

            </fr.ganfra.materialspinner.MaterialSpinner>
            <!--
            <Spinner
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/JN_04_02"
                style="@style/Widget.AppCompat.Light.DropDownItem.Spinner"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:background="@drawable/abc_spinner_mtrl_am_alpha"
                android:spinnerMode="dialog" />
    -->
            <!--<TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/QtdDos"
                android:id="@+id/textView3"
                style="@style/Textos"
                android:layout_marginTop="15dp" />
    -->
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp">

                <EditText
                    android:id="@+id/JN_04_03"
                    android:layout_width="97dp"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="Doses*"
                    android:inputType="numberDecimal" />
            </android.support.design.widget.TextInputLayout>

            <!--style="@style/CamposImput"-->

            <View

                style="@style/Divider" />

            <RelativeLayout xmlns:custom="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:gravity="center_horizontal">

                <Button
                    android:id="@+id/btnAdd"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_marginEnd="5dp"
                    android:layout_toStartOf="@+id/btnRemove"
                    android:background="@color/cardview_light_background"
                    android:singleLine="false"
                    android:text="@string/ADD"
                    android:textColor="@color/colorPrimary" />
                <!--<com.getbase.floatingactionbutton.FloatingActionButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/btnAdd"
                    fab:fab_colorNormal="#ffffff"
                    fab:fab_colorPressed="#ffffff"
                    fab:fab_size="normal"
                    fab:fab_icon="@drawable/ic_action_confirm"

                    />
                    -->
                <Button
                    android:id="@+id/btnRemove"
                    android:layout_width="120dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:background="@color/colorAccent"

                    android:text="@string/REMOVE"
                    android:textColor="@color/colorTextButton" />
            </RelativeLayout>


            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp">

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

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <ListView
                            android:id="@+id/JN_04_06"
                            android:layout_width="210dp"
                            android:layout_height="190dp"
                            android:layout_alignParentLeft="true"
                            android:layout_alignParentStart="true"
                            android:layout_centerVertical="true"
                            android:divider="@color/colorAccentSecurdary" />

                        <ListView
                            android:id="@+id/JN_04_07"
                            android:layout_width="140dp"
                            android:layout_height="190dp"
                            android:layout_alignParentEnd="true"
                            android:divider="@color/colorAccentSecurdary" />
                    </RelativeLayout>
                </ScrollView>
            </android.support.design.widget.TextInputLayout>
            <!--style="@style/CamposImput"-->


            <!--
            <RelativeLayout
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:gravity="center_horizontal">
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/cancelar"
                    android:id="@+id/JN_04_11"
                    android:onClick="JN_04_Fechar"
                    style="@style/Botoes"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true" />

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/confirma"
                    android:id="@+id/JN_04_12"
                    android:onClick="clickBtnProg"

                    style="@style/Botoes"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentEnd="true" />

            </RelativeLayout>
            -->
            <View style="@style/Divider" />

            <TextView
                style="@style/Textos"
                android:layout_width="match_parent"

                android:layout_height="match_parent"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp"
                android:text="@string/Repet" />

            <RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp">

                <CheckBox
                    android:id="@+id/chkUmaVez"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="@string/somenteUma" />

                <CheckBox
                    android:id="@+id/chkTodosDias"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="false"
                    android:text="@string/todosDias" />

                <RelativeLayout
                    android:layout_width="193dp"
                    android:layout_height="match_parent">

                    <TextView
                        android:id="@+id/textView"
                        style="@style/Textos"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:layout_centerVertical="true"
                        android:text="@string/Repetir"
                        android:textAppearance="?android:attr/textAppearanceMedium" />

                    <EditText
                        android:id="@+id/horasRep"
                        android:layout_width="30dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentTop="true"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:layout_toEndOf="@+id/textView"
                        android:ems="2"
                        android:inputType="number"
                        android:maxLength="2"
                        android:maxLines="1"
                        android:textAlignment="center" />

                    <TextView
                        android:id="@+id/textView22"
                        style="@style/Textos"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/textView"
                        android:layout_toEndOf="@+id/horasRep"
                        android:text="@string/horas"
                        android:textAppearance="?android:attr/textAppearanceMedium" />
                </RelativeLayout>
            </RadioGroup>

            <TextView
                android:id="@+id/textView12"
                style="@style/Textos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
                android:paddingRight="?android:attr/listPreferredItemPaddingRight"
                android:paddingTop="24dp"
                android:text="@string/CampoObrigatorio"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/colorAccent" />
        </LinearLayout>
    </ScrollView>

</RelativeLayout>

1 answer

1


First, let’s understand what’s going on with your layout.

When you use the RelativeLayout, you’re putting your components all together, and then telling them how to behave (via attributes like toRightOf or alignParentRight etc..).

In your case, you’re putting your Toolbar and soon after putting your ScrollView.

Basically, what’s going on is that your Toolbar is behind of your ScrollView, which explains why it is not appearing on the screen. Remember that, the order you declare the components in your xml, will be the order that your RelativeLayout will draw/layout. In other words: your first Toolbar and then, on top, your ScrollView.

You have some ways to solve this, being the first least indicated and the last plus nominee:

1) You can arrange your layout differently: putting your ScrollView and then your Toolbar for example,

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

    <ScrollView .../>

    <android.support.v7.widget.Toolbar .../>

</RelativeLayout>

2) You can keep your layout the way it is and define how your components will behave, ie setting the attributes that will define how your components will position themselves within your RelativeLayout: defining the attribute android:layout_below="" in his ScrollView, telling him to stand up down below other layout (id), for example,

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

    <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar"
        .../>

    <ScrollView 
        ...
        android:layout_below="@+id/toolbar"/>

</RelativeLayout>

3) You can replace your RelativeLayout for a LinearLayout with the guidance vertical. This will cause your layout to be constructed in a linear way and respecting the order. Assigning the attribute android:layout_weight="1", you will ensure that your ScrollView will take up all the size she’ll need:

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1">
        ...
    </ScrollView>

</LinearLayout>
  • 1

    Very good guy! Thanks even for your explanation! I used the second option, it was more appropriate for my case! Thank you very much!!!!

Browser other questions tagged

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