【问题标题】:Toolbar overlaps other content in Android工具栏与 Android 中的其他内容重叠
【发布时间】:2021-10-15 05:31:12
【问题描述】:

我正在尝试使用自定义工具栏开发应用程序,它与屏幕上的其他内容重叠。我有一个回收站视图,用于显示带有文本的卡片视图。

在设计视图中似乎没问题,并显示回收器视图位于工具栏下方且通畅。

Android Studio Design view shows the toolbar not obstructing the recycler view.

但是,当启动应用程序时,它明显重叠。

The app is launched and the content is hidden behind the toolbar.

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>

工具栏.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

【问题讨论】:

  • activity_main 屏幕中的更多详细信息
  • 对不起,如果你在说什么,我只是省略了activity_main中布局的结束标记。

标签: android android-layout toolbar android-toolbar


【解决方案1】:

您可以尝试使用CoordinatorLayout 而不是RelativeLayout 作为父级。看看这个tutorial

ConstraintLayout 发布 you should not use RelativeLayout,因为它更慢且功能更弱。

注意:CoordinatorLayoutConstraintLayout 不是同一个东西,并且有非常不同的用例。

【讨论】:

    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多