2016-02-27 43 views
0

ñ我的主要活动我有一个导航抽屉,并通过点击它我加载不同的片段,如首页,购物车,设置等。最初在我的主要活动,我得到了工具栏与微调并按钮导航抽屉以及。但对于其他片段,我需要根据需要自定义工具栏。编辑工具栏根据片段显示

这里是我的activity_main.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/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> 

然后,我实现的方法根据片段加载自定义工具栏(主要活动内):

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。任何建议,使这项工作将不胜感激。谢谢。

回答

1

这个问题有很多解决方案。

如果您有不同的工具栏,你可以添加设置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); 

从主要活动获取工具栏并进行编辑。

+0

很好的答案Hoang Nguyen。我用你的第一个解决方案做到了。我不知道'toolbar.setVisibility(View.VISIBLE);'。我只是Android开发的初学者。我之前正在尝试使用第二种解决方案。但是,如果我没有在main_activity中包含工具栏,它总是会给我一个空指针异常。一旦我包含了,我的显示器中会出现多个工具栏。无论如何非常感谢解决方案。 :) –

+1

没问题。使用第二种解决方案,它会得到您在主要活动中包含的工具栏,您可以像这样更改视图: http://stackoverflow.com/questions/3334048/android-layout-replacing-a-view-with-another -view-on-run-time 对不起,我忘了它。 –