Layout 启动另一个活动并展开新布局后,MainActivity的布局仍然可见

Layout 启动另一个活动并展开新布局后,MainActivity的布局仍然可见,layout,overlapping,Layout,Overlapping,我想通过使用活动而不是片段在我的应用程序中使用导航抽屉。通过在不同的活动中使用布局充气器,我试图保持与MainActivity中相同的导航抽屉,但改变了内部布局。我的问题是,不知何故,主活动的布局无法替换。单击导航抽屉中的项目时,将打开新活动并启动LayoutFlater。但是主活动的布局仍然保留,而其他活动的布局只是添加了 public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelecte

我想通过使用活动而不是片段在我的应用程序中使用导航抽屉。通过在不同的活动中使用布局充气器,我试图保持与MainActivity中相同的导航抽屉,但改变了内部布局。我的问题是,不知何故,主活动的布局无法替换。单击导航抽屉中的项目时,将打开新活动并启动LayoutFlater。但是主活动的布局仍然保留,而其他活动的布局只是添加了

public class MainActivity extends AppCompatActivity implements 
TabLayout.OnTabSelectedListener{
public static String TAG = "MainActivity";

DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "CREATED");



NavigationView navigationView = (NavigationView) 
findViewById(R.id.navigation_view);

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, ^ 
toolbar, R.string.drawer_open, R.string.drawer_closed);
drawerLayout.setDrawerListener(actionBarDrawerToggle);

navigationView.setNavigationItemSelectedListener(new 
NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case R.id.Mensa_in_der_Nähe:
                Intent anIntent = new Intent(getApplicationContext(), 
    LocationActivity.class);
                startActivity(anIntent);
                drawerLayout.closeDrawers();
                break;

        }
        return false;
     }
    });
然后是主活动的xml文件:

 <android.support.v4.widget.DrawerLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/drawerLayout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:clickable="true"
 android:focusableInTouchMode="true">



 <android.support.design.widget.CoordinatorLayout
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/overview_coordinator_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="enterAlways|scroll"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <android.support.design.widget.TabLayout
            android:id="@+id/menu_tabs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:tabMode="fixed" />

        <android.support.design.widget.TabLayout
            android:id="@+id/canteen_tabs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:tabMode="scrollable" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/dish_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </android.support.v4.view.ViewPager>

   </FrameLayout>

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

 <android.support.design.widget.NavigationView
 android:id="@+id/navigation_view"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_gravity="start"
 android:layout_marginTop="?attr/actionBarSize"
 android:background="@android:color/white"
 app:menu="@menu/drawer_menu" />
 public class LocationActivity extends MainActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 FrameLayout contentFrameLayout = (FrameLayout) 
 findViewById(R.id.content_frame);
 getLayoutInflater().inflate(R.layout.activity_location, 
 contentFrameLayout);
  }}
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent" android:layout_height="match_parent">

<TimePicker
  android:id="@+id/timePicker"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
</FrameLayout>
以及其他活动的xml文件:

 <android.support.v4.widget.DrawerLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/drawerLayout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:clickable="true"
 android:focusableInTouchMode="true">



 <android.support.design.widget.CoordinatorLayout
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:id="@+id/overview_coordinator_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="enterAlways|scroll"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <android.support.design.widget.TabLayout
            android:id="@+id/menu_tabs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:tabMode="fixed" />

        <android.support.design.widget.TabLayout
            android:id="@+id/canteen_tabs"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:tabMode="scrollable" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/dish_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </android.support.v4.view.ViewPager>

   </FrameLayout>

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

 <android.support.design.widget.NavigationView
 android:id="@+id/navigation_view"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout_gravity="start"
 android:layout_marginTop="?attr/actionBarSize"
 android:background="@android:color/white"
 app:menu="@menu/drawer_menu" />
 public class LocationActivity extends MainActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 FrameLayout contentFrameLayout = (FrameLayout) 
 findViewById(R.id.content_frame);
 getLayoutInflater().inflate(R.layout.activity_location, 
 contentFrameLayout);
  }}
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent" android:layout_height="match_parent">

<TimePicker
  android:id="@+id/timePicker"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
</FrameLayout>