2017-07-25 42 views
0

我將最小設置爲4.4的Android版本5.0作爲目標。我想創建一個簡單的側面菜單,用戶可以選擇應用程序的選項/頁面。如何用Xamarin.Android創建漢堡面板/側抽屜? (5.0+)

我現在面臨的問題是在各種博客,論壇和文檔頁面的這個推薦的解決方案的數量。其中許多需要下載各種組件和附加庫,以及支持庫以實現向後兼容。

我可以使用什麼方法,如果我不需要向後兼容,只是想使與醜陋的新材料設計考慮到Android應用程序?

是否有甚至內置的的組件?或者是下載這些庫,併爲他們做了大量的設置,我有最好的選擇?

回答

1

是否有甚至內置的的組件?或者是下載這些庫,併爲他們做了大量的設置,我有最好的選擇?

NavigationViewDrawerLayout實際上是實現可滑動漢堡面板的正式推薦方式。但正如你所說,要使用它,需要安裝Material Design(Android支持設計庫)。

如果你不想使用它。實際上有一種方法可以直接使用Fragment來實現側抽屜。例如:

在你活動的佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
       android:id="@+id/sidedrawer" 
       android:orientation="vertical" 
       android:layout_height="match_parent" 
       android:layout_width="wrap_content" 
       android:background="@drawable/drawerborder"> 
    <Button android:id="@+id/home" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="HOME" /> 
    <Button android:id="@+id/settings" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="SETTINGS" /> 
    </LinearLayout> 
    <FrameLayout android:id="@+id/container" 
       android:layout_height="match_parent" 
       android:layout_width="wrap_content" /> 
</LinearLayout> 

後面的代碼:

protected override void OnCreate(Bundle savedInstanceState) 
{ 
    base.OnCreate(savedInstanceState); 

    // Create your application here 
    SetContentView(Resource.Layout.layout1); 

    Button home = FindViewById<Button>(Resource.Id.home); 
    home.Click += (sender, e) => 
    { 
     FragmentTransaction transaction = this.FragmentManager.BeginTransaction(); 
     HomeFragment homefragment = new HomeFragment(); 
     transaction.Replace(Resource.Id.container, homefragment).Commit(); 
    }; 

    Button settings = FindViewById<Button>(Resource.Id.settings); 
    settings.Click += (sender, e) => 
    { 
     FragmentTransaction transaction = this.FragmentManager.BeginTransaction(); 
     SettingsFragment settingsfragment = new SettingsFragment(); 
     transaction.Replace(Resource.Id.container, settingsfragment).Commit(); 
    }; 
} 

我沒加手勢和動畫的sidedrawer,使其滑動。你可以自己嘗試。

但我不能告訴哪種方式更容易,在我看來,這些安裝包將更加方便。要使用我上面提到的方法,許多作品需要由我們自己完成。例如,滑入/滑出動畫,手勢識別,甚至抽屜的邊框。所以是的,我個人認爲下載這些庫是最好的選擇。

編輯: 我忘記說了,如果你想有一個彈出側的抽屜裏,您可以嘗試使用自定義對話框。

+0

好吧,我明白了。我會試着嘗試兩種選擇。謝謝! – Reynevan

+0

我最終下載了Android支持庫,現在正在試驗。我設法創建了一個工具欄,但是所有的教程都說我應該把這個圖標設置爲'Resource.Drawable.ic_menu'的行,它不存在。我錯過了什麼,或者是我必須創建自己的文件? – Reynevan

相關問題