Панель инструментов перекрывает другой контент в Android

#android #android-layout #toolbar #android-toolbar

Вопрос:

Я пытаюсь разработать приложение с помощью пользовательской панели инструментов, и оно перекрывает другой контент на экране. У меня есть вид переработчика, который я использую для отображения видов карточек с текстом на них.

В режиме конструктора это выглядит нормально и показывает, что вид переработчика находится под панелью инструментов и свободен.

Вид дизайна Android Studio показывает панель инструментов, не мешающую просмотру переработчика.

Однако при запуске приложения оно явно перекрывается.

Приложение запускается, и содержимое скрыто за панелью инструментов.

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp">

<include
    android:id="@ id/toolbar"
    layout="@layout/toolbar"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />

<androidx.recyclerview.widget.RecyclerView
    android:id="@ id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/toolbar"
    app:layout_constraintTop_toBottomOf="@id/toolbar"
    android:background="@android:color/darker_gray"
    android:padding="4dp"
    android:scrollbars="vertical"
    />
</RelativeLayout>
 

toolbar.xml

 <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/purple_200"
android:elevation="4dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:id="@ id/back_icon"
        android:src="@drawable/ic_baseline_arrow_back_24" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_toEndOf="@id/back_icon"
        android:layout_centerVertical="true"
        android:text="@string/inventory_menu"
        android:textSize="20sp"
        android:textColor="@color/white"/>

    <ImageView
        android:id="@ id/add_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toStartOf="@id/delete_icon"
        android:layout_marginEnd="30dp"
        android:src="@drawable/ic_baseline_add_circle_outline_24" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentEnd="true"
        android:id="@ id/delete_icon"
        android:src="@drawable/ic_baseline_delete_24"/>
</RelativeLayout>
</androidx.appcompat.widget.Toolbar>
 

example_item.xml

 <?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="4dp"
android:layout_marginBottom="4dp">


<RelativeLayout
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="4dp">

    <TextView
        android:id="@ id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Line1"
        android:textSize="20sp"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@ id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Line 2"
        android:textSize="15sp"
        android:layout_below="@ id/textView"
        android:layout_marginStart="8dp"/>


</RelativeLayout>
</androidx.cardview.widget.CardView>enter code here
 

Комментарии:

1. более подробная информация на экране activity_main

2. Извините, я просто опустил закрывающий тег для макета в activity_main, если вы об этом говорили.

Ответ №1:

Вы можете попробовать использовать a CoordinatorLayout вместо RelativeLayout в качестве родителя. Посмотрите на этот учебник.

С ConstraintLayout выпущенным вы не должны использовать RelativeLayout , так как он медленнее и менее работоспособен.

Примечание: CoordinatorLayout и ConstraintLayout это не одно и то же, и у них очень разные варианты использования.