首页 > 解决方案 > Inheritance in Xamarin Android

问题描述

I use Xamarin for Android. I have two activities with some content, there is android.support.v4.widget.DrawerLayout and android.support.design.widget.NavigationView in XML files.

In MainActivity I use two object for content control. These are DrawerLayout and NavigationView. To switch between activities I use own method.

private void OnNavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
    {
        switch (e.MenuItem.ItemId)
        {
            case (Resource.Id.home):
                //Go home

                break;

            case (Resource.Id.settings):
                //Go to other activity
                break;
        }

        drawerLayout.CloseDrawers();
    }

DrawerLayout, NavigationView and OnNavigationItemSelected is located in MainActivity, but I've created one more activities. This is Settings and when I chose settings item, I'm moving to Settings activity.

I wrote the same implementation in Settings activity. I have DrawerLayout, NavigationView and OnNavigationItemSelected.

It turns out that I just copied code. Maybe I should use inheritance? I just don't understand how can I realize that in Android application.

标签: androidxamarininheritance

解决方案


为每个活动之间的共享创建通用 layout.xml。

  1. 放入一个我们称之为 xml 的文件DrawerLayout中。NavigationViewA.xml

  2. 包含A.xml在主页和设置的 xml 中。

例子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <include
        layout="@layout/A"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

检查https://stackoverflow.com/a/17133102/8187800


推荐阅读