无意中重叠 Android 视图

Unintentionally Overlapping Android Views

我的 xml 文件使用 android DrawerLayout。它在打开我想要的应用程序时显示工具栏和 RecyclerView,但是当我打开抽屉并单击一个选项时,单击的 RecyclerView 和片段重叠。

我的代码

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
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:id="@+id/profileDrawer"
android:layout_height="match_parent" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/robot_chooser_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content_frame2">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/robot_recycler_view"
        android:paddingTop="5dp"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TextView
        android:id="@+id/robot_empty_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textSize="20sp"
        android:text="@string/no_robots"
        android:elevation="3dp"
        android:layout_gravity="center"
        android:visibility="gone"
        android:gravity="center" />
    </FrameLayout>

</LinearLayout>

<ListView
    android:id="@+id/left_drawer2"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#eee"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:listSelector="@android:color/darker_gray" />

</android.support.v4.widget.DrawerLayout>

[IMG]http://i63.tinypic.com/157fbps.png[/IMG]

我在我的 main activity

中使用这一行将片段放在前面
fragmentManager.beginTransaction().replace(R.id.content_frame2, fragment).commit();

您的问题是 activity 有多个容器:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/content_frame2">

<android.support.v7.widget.RecyclerView
    android:id="@+id/robot_recycler_view"
    android:paddingTop="5dp"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<TextView
    android:id="@+id/robot_empty_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textSize="20sp"
    android:text="@string/no_robots"
    android:elevation="3dp"
    android:layout_gravity="center"
    android:visibility="gone"
    android:gravity="center" />
</FrameLayout>

您正在 FrameLayout 中加载您的片段,但您仍然有 RecyclerView。您应该只在 activity XML 中有一个 FrameLayout,您的片段将加载 RecyclerView、TextView 或您想要的任何内容。就像现在一样,您似乎在将片段加载到 FrameLayout 中,同时将 RecyclerView 留在原地,这会导致重叠。

你的布局应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/profileDrawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <android.support.v7.widget.Toolbar
            android:id="@+id/robot_chooser_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <FrameLayout
            android:id="@+id/content_frame2"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <ListView
        android:id="@+id/left_drawer2"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#eee"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:listSelector="@android:color/darker_gray" />

</android.support.v4.widget.DrawerLayout>