根据显示的片段编辑工具栏
Edit toolbar according to the fragment displaying
在我的主 activity 我有一个导航抽屉,通过单击它我正在加载不同的片段,如 Home 、 Cart 、 Settings 等。最初在我的主activity 我还得到了带有微调器和导航抽屉按钮的工具栏。但对于其他片段,我需要根据需要自定义工具栏。
Here is how my activity_main.xml implemented :
<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/navigation_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="@bool/fitsSystemWindows">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_kitkat_height"
android:background="?colorPrimary" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_lollipop_height"
android:background="?colorPrimaryDark" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/status_bar_margin_top">
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?actionBarSize" />
<!--include different toolbars into the activity dont know this is correct implementation... If I missed any of them It gives me a null pointer exception in MainActivity..-->
<include
android:id="@+id/toolbar_cart"
layout="@layout/toolbar_cart" />
<include
android:id="@+id/toolbar_home"
layout="@layout/toolbar_home" />
</FrameLayout>
<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:fitsSystemWindows="@bool/fitsSystemWindows"
app:headerLayout="@layout/navigation_drawer_header"
app:menu="@menu/navigation_drawer_menu"
app:theme="@style/NavigationViewTheme" />
</android.support.v4.widget.DrawerLayout>
然后我实现了一个根据片段加载自定义工具栏的方法(inside main activity):
public void customizeToolBar(int position) {
switch (position) {
case 0:
toolbar = (Toolbar) findViewById(R.id.toolbar_home);
setSupportActionBar(toolbar);
subCategories_spinner = (Spinner) findViewById(R.id.spinner_subCategories);
actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
break;
case 1:
toolbar = (Toolbar) findViewById(R.id.toolbar_cart);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setTitle("Check Out");
actionBar.setDisplayHomeAsUpEnabled(true);
break;
....
然后我在 NavigationItemSelectedListener 中调用该方法,如下所示:
private void setupNavigationDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.item_navigation_drawer_home:
menuItem.setChecked(true);
customizeToolBar(0);
setFragment(0);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_navigation_drawer_cart:
menuItem.setChecked(true);
customizeToolBar(1);
setFragment(1);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
...
但这不起作用。每次显示相同的工具栏。我是 android 的新手。任何使这项工作的建议将不胜感激。谢谢你。
这个问题有很多解决方案。
如果您包含不同的工具栏,您可以添加集 android:visibility=""
例如:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#2196F3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
</android.support.v7.widget.Toolbar>
并在更改片段时设置工具栏可见性:
toolbar.setVisibility(View.VISIBLE);
或者您可以在片段中编辑工具栏:
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
从 Main Activity 获取工具栏并编辑。
在我的主 activity 我有一个导航抽屉,通过单击它我正在加载不同的片段,如 Home 、 Cart 、 Settings 等。最初在我的主activity 我还得到了带有微调器和导航抽屉按钮的工具栏。但对于其他片段,我需要根据需要自定义工具栏。
Here is how my activity_main.xml implemented :
<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/navigation_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="@bool/fitsSystemWindows">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_kitkat_height"
android:background="?colorPrimary" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="@dimen/status_bar_lollipop_height"
android:background="?colorPrimaryDark" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/status_bar_margin_top">
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?actionBarSize" />
<!--include different toolbars into the activity dont know this is correct implementation... If I missed any of them It gives me a null pointer exception in MainActivity..-->
<include
android:id="@+id/toolbar_cart"
layout="@layout/toolbar_cart" />
<include
android:id="@+id/toolbar_home"
layout="@layout/toolbar_home" />
</FrameLayout>
<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:fitsSystemWindows="@bool/fitsSystemWindows"
app:headerLayout="@layout/navigation_drawer_header"
app:menu="@menu/navigation_drawer_menu"
app:theme="@style/NavigationViewTheme" />
</android.support.v4.widget.DrawerLayout>
然后我实现了一个根据片段加载自定义工具栏的方法(inside main activity):
public void customizeToolBar(int position) {
switch (position) {
case 0:
toolbar = (Toolbar) findViewById(R.id.toolbar_home);
setSupportActionBar(toolbar);
subCategories_spinner = (Spinner) findViewById(R.id.spinner_subCategories);
actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
break;
case 1:
toolbar = (Toolbar) findViewById(R.id.toolbar_cart);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setTitle("Check Out");
actionBar.setDisplayHomeAsUpEnabled(true);
break;
....
然后我在 NavigationItemSelectedListener 中调用该方法,如下所示:
private void setupNavigationDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.item_navigation_drawer_home:
menuItem.setChecked(true);
customizeToolBar(0);
setFragment(0);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
case R.id.item_navigation_drawer_cart:
menuItem.setChecked(true);
customizeToolBar(1);
setFragment(1);
drawerLayout.closeDrawer(GravityCompat.START);
return true;
...
但这不起作用。每次显示相同的工具栏。我是 android 的新手。任何使这项工作的建议将不胜感激。谢谢你。
这个问题有很多解决方案。
如果您包含不同的工具栏,您可以添加集 android:visibility=""
例如:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#2196F3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone">
</android.support.v7.widget.Toolbar>
并在更改片段时设置工具栏可见性:
toolbar.setVisibility(View.VISIBLE);
或者您可以在片段中编辑工具栏:
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
从 Main Activity 获取工具栏并编辑。