感谢上面的回答,我成功地做到了我想做的事情,以下是我为任何未来寻找此功能的人所做的准确步骤:
我的activity_main.xml如下:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content_frame"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_drawer_header"
app:menu="@menu/drawer_menu">
</android.support.design.widget.NavigationView>
这是标准的DrawerLayout,它汇集了所有NavigationDrawer菜单所需的组件和元素。重要的补充是FrameLayout...需要给它一个ID叫做content_frame。其他活动所使用的所有布局都将被推送/添加/填充到此处。
我的BaseActivity.java如下:
package com.example.test;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
public class BaseActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
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.menu_home:
Intent anIntent = new Intent(getApplicationContext(), TheClassYouWantToLoad.class);
startActivity(loadPlayer);
drawerLayout.closeDrawers();
break;
}
return false;
}
});
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
}
现在,在onNavigationItemSelected方法内部,有一个switch语句来处理每个菜单项被选中时会发生什么。这是重要的部分:
Intent anIntent = new Intent(getApplicationContext(), TheClassYouWantToLoad.class);
startActivity(anIntent);
drawerLayout.closeDrawers();
您需要将"TheClassYouWantToLoad"替换为您自己的类名。
现在,在这个新类中(它可能需要加载一些新的UI元素),您需要以下内容:
public class TheClassYouWantToLoad extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame);
getLayoutInflater().inflate(R.layout.the_layout_you_want_to_load, contentFrameLayout);
}
}
并将 "the_layout_you_want_to_load" 替换为您想要加载的布局名称。