我的项目中有2个Actitivity。 MainActivity中有一个NavigationDrawer(我的第一个活动)。我可以使用汉堡包图标访问导航菜单项。但我想在我的TrafficActivity中访问它。如何添加汉堡包图标并访问导航菜单项。
my.xml
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.yunus.ototakip.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:titleTextColor="@color/beyaz"
app:subtitleTextColor="@color/beyaz">
</android.support.v7.widget.Toolbar>
<include layout="@layout/content_main" />
MainActivity.java
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_kullanici) {
} else if (id == R.id.nav_hatirlatma)
{
}
else if (id == R.id.nav_ipucu)
{
startActivity(new Intent(MainActivity.this,IpuclariSayfasi.class));
}
else if (id == R.id.nav_oyla) {
}
else if (id == R.id.nav_cikis)
{
//startActivity(new Intent(MainActivity.this,GirisActivity.class));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
答案 0 :(得分:1)
在您的情况下,建议使用活动的片段:
将所有MainActivity内容(中间内容,非DrawerLayout或NavigationView Stuff )移动到名为MainFragment(ex)的片段中:
将frameLayout添加到out content_main.xml:
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/widget_title_id"
android:background="@android:color/darker_gray"/>
并将其替换为MainActivity onCreate:
中的MainFragment <FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
为IpuclariSayfasi活动创建另一个片段。
OnNavigationItemSelected()用IpuclariSayfasi片段替换MainFragment:
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new MainFragment()).commit();
这是使用导航抽屉活动的标准系统。