Android 在导航视图中显示并同步来自片段的汉堡图标?
这是我的Android 在导航视图中显示并同步来自片段的汉堡图标?,android,android-fragments,android-toolbar,Android,Android Fragments,Android Toolbar,这是我的活动\u main.xml。它有一个导航视图和框架布局 <?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
活动\u main.xml
。它有一个导航视图和框架布局
<?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/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_container">
<include layout="@layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
处理导航抽屉中的点击项目:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the camera action
} else if (id == R.id.nav_updates) {
UpdatesFragment fragment = new UpdatesFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_container,fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} .. and so on
UpdatesFragment
具有与工具栏一起的视图。我的意思是我在为每一个片段重复工具栏
在UpdatesFragment
的onCreateView
方法中,我使用此代码显示我专门为UpdatesFragment
编写的工具栏
Toolbar toolbar = (Toolbar)view.findViewById(R.id.toolbar);
AppCompatActivity activity = ((AppCompatActivity) getActivity());
activity.setSupportActionBar(toolbar);
我的问题是:
Toolbar toolbar = (Toolbar)view.findViewById(R.id.toolbar);
AppCompatActivity activity = ((AppCompatActivity) getActivity());
activity.setSupportActionBar(toolbar);