2017-06-18 52 views
0

我正在嘗試使用Visual Studio 2017在Xamarin中創建導航抽屜。 我在谷歌的一些研究之後創建了導航抽屜。但是當我嘗試從菜單中打開活動時,導航抽屜會消失。Xamarin中的多個活動中的導航抽屜Android

我想在所有活動中使用我的導航抽屜,而不是在所有活動中重複它。

鏈接到我的項目:https://github.com/Chindara/XamarinAndroidNavigationDrawer

+0

你,當你打開一個活動,導航抽屜仍然 上的活動顯示是什麼意思? –

+0

@YorkShen否。當我從導航抽屜打開活動時,活動將在沒有導航抽屜的情況下打開。 – Chindara

+0

對不起,我沒有真正理解這句話的意思,「我的所有活動中都有導航抽屜,沒有在所有活動中重複」,你能解釋一下嗎? –

回答

1

當您單擊菜單項,就應該導航到Fragment而不是ActivityFramework還可以顯示用戶界面。如果你這樣做,當你選擇Fragmentnavigation drawer將永遠在你的Activitiy,如this。以下是關於如何在Xamarin中使用Fragmentdocument

當您選擇的菜單項:

navigationView.NavigationItemSelected += (sender, e) => 
     { 
      e.MenuItem.SetChecked(true); 

      FragmentTransaction ft = this.FragmentManager.BeginTransaction(); 
      if (e.MenuItem.ItemId == Resource.Id.nav_MGrade) 
      { 
       MGradeFragment mg = new MGradeFragment(); 
       // The fragment will have the ID of Resource.Id.fragment_container. 
       ft.Replace(Resource.Id.ll, mg); 
      } 
      else if(e.MenuItem.ItemId == Resource.Id.home) 
      { 
       //... 
      } 
      //... 

      // Commit the transaction. 
      ft.Commit(); 
      drawerLayout.CloseDrawers(); 
     }; 

MGradeFragment.class,做同樣的事情,你MGradeActivity

public class MGradeFragment : Fragment 
{ 
    public override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     // Create your fragment here 
    } 

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     // Use this to return your custom view for this Fragment 
     // return inflater.Inflate(Resource.Layout.YourFragment, container, false); 

     View view = LayoutInflater.From(Activity).Inflate(Resource.Layout.MGradesView, null); 
     return view; 
    } 
} 

MGradeFragment將取代LinearLayout在Main.axml:

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:id="@+id/ll" 
     > 
     <include 
      layout="@layout/toolbar" /> 
    </LinearLayout> 

而且在你的toolbar.axml,修改代碼:

<android.support.design.widget.CoordinatorLayout 
    .... 
    android:id="@+id/main_content" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">//Change match_parent to wrap_content 
+0

非常感謝@ york-shen。非常有用的詳細答案。我已經用解決方案更新了我的GitHub項目。 – Chindara