【问题标题】:Inheritance in Xamarin AndroidXamarin Android 中的继承
【发布时间】:2023-03-04 19:30:01
【问题描述】:

我使用适用于 Android 的 Xamarin。我有两个包含一些内容的活动,XML 文件中有 android.support.v4.widget.DrawerLayoutandroid.support.design.widget.NavigationView

在 MainActivity 我使用两个对象进行内容控制。它们是DrawerLayoutNavigationView。要在活动之间切换,我使用自己的方法。

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();
    }

DrawerLayoutNavigationViewOnNavigationItemSelected 位于 MainActivity,但我又创建了一个活动。这是Settings,当我选择设置项时,我将转到Settings 活动。

我在Settings 活动中编写了相同的实现。我有DrawerLayoutNavigationViewOnNavigationItemSelected

原来我只是复制了代码。也许我应该使用继承?我只是不明白如何在 Android 应用程序中实现这一点。

【问题讨论】:

    标签: android xamarin inheritance


    【解决方案1】:

    创建共同的 layout.xml 以便在每个活动之间共享。

    1. DrawerLayoutNavigationView 放入一个xml,我们称之为A.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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多