我想通过使用活动而不是片段在我的应用程序中使用导航抽屉。通过在不同的活动中使用Layout inflater,我试图保持与MainActivity中相同的导航抽屉,但是要更改内部布局。我的问题是,不可能以某种方式替换MainActivity的布局。单击导航栏中的项目时,新的Activtiy将打开并启动layoutinflater。但主要活动的布局仍然存在,而其他活动的布局刚刚添加。
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);
}}
另一项活动的xml文件:
<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>