【问题标题】:C# Android: Error Class cannot have multiple base classes: 'Android.Support.V7.App.ActionBarActivity' and 'TabHostAct'C# Android:错误类不能有多个基类:“Android.Support.V7.App.ActionBarActivity”和“TabHostAct”
【发布时间】:2016-07-26 05:13:50
【问题描述】:

我正在尝试在一个类中扩展两个活动。我知道这是不可能的,我尝试过像这样制作两个不同的类:

[Activity(Label = "DLS", Theme = "@style/MyTheme", ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
public class ActProduct : ActionBarActivity, TabHostAct
{
        base.OnCreate(savedInstanceState);

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

        mToolbar = FindViewById<SupportToolbar>(Resource.Id.toolbar);
        mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
        mLeftDrawer = FindViewById<ListView>(Resource.Id.left_drawer);

        mLeftDrawer.Tag = 0;

        SetSupportActionBar(mToolbar);

        mLeftDataSet = new List<string>();
        mLeftDataSet.Add("Login");
        mLeftDataSet.Add("Camera");
        mLeftDataSet.Add("Maps");
        mLeftDataSet.Add("Product");
        mLeftAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mLeftDataSet);
        mLeftDrawer.Adapter = mLeftAdapter;
        mLeftDrawer.ItemClick += MenuListView_ItemClick;

        mDrawerToggle = new MyActionBarDrawerToggle(
            this,                           //Host Activity
            mDrawerLayout,                  //DrawerLayout
            Resource.String.openDrawer,     //Opened Message
            Resource.String.closeDrawer     //Closed Message
        );

        mDrawerLayout.SetDrawerListener(mDrawerToggle);
        SupportActionBar.SetHomeButtonEnabled(true);
        SupportActionBar.SetDisplayShowTitleEnabled(true);
        mDrawerToggle.SyncState();
}

public class TabHostAct : TabActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        CreateTab(typeof(ActProProduk), "Prod", "Product", Resource.Drawable.ic_tab_product);
        CreateTab(typeof(ActProCustomer), "Cust", "Customer", Resource.Drawable.ic_tab_customer);
        CreateTab(typeof(ActProPromo), "Promo", "Promotion", Resource.Drawable.ic_tab_promo);
    }

    private void CreateTab(Type activityType, string tag, string label, int drawableId)
    {
        var intent = new Intent(this, activityType);
        intent.AddFlags(ActivityFlags.NewTask);

        var spec = TabHost.NewTabSpec(tag);
        var drawableIcon = Resources.GetDrawable(drawableId);
        spec.SetIndicator(label, drawableIcon);
        spec.SetContent(intent);

        TabHost.AddTab(spec);
    }
}

这是我的 .axml 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Dark" />
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <!-- The Main Content View -->
        <FrameLayout
            android:id="@+id/main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="25dp"
                android:layout_marginLeft="25dp">
                <TabHost
                    android:id="@android:id/tabhost"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:padding="5dp">
                        <TabWidget
                            android:id="@android:id/tabs"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content" />
                        <FrameLayout
                            android:id="@android:id/tabcontent"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:padding="5dp" />
                    </LinearLayout>
                </TabHost>
            </LinearLayout>
        </LinearLayout>
    <!-- The Left Navigation Drawer -->
        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="#818181"
            android:dividerHeight="1dp"
            android:background="#E3F2FD" />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

我的问题有什么解决办法吗?提前谢谢你。

【问题讨论】:

  • i know it is not possible 既然知道没有解决方案,为什么还要寻求解决方案?
  • 我从来没有说过我已经知道我的问题是否没有解决方案。我只是想说用我的方法不可能做到这一点。我正在努力寻找可以为我解决问题的人。

标签: c# android


【解决方案1】:

不要使用TabActivity。相反,请在您的布局中包含 TabHost

【讨论】:

  • 很抱歉,我不太明白你的意思。我已经在我的布局中使用 TabHost。 TabActivity 是为了让在类内运行的 TabHost 可运行。或许你可以给我代码示例?
  • stackoverflow.com/questions/20469877/… 请注意,AppCompatActivity 还提供了 Fragmentmanager。
  • 我创建的每个抽屉都有不同的活动。因此,为我的抽屉创建片段是不可能的,但是我目前正在尝试将选项卡布局功能变成片段,因为我认为它使用相同的活动,即“ActionBarActivity”,但是有一个错误说:'ActionBar'是 'Android.App.ActionBar' 和 'Android.Support.V7.App.ActionBar' 之间的模棱两可的引用,所以现在,我又回到了死胡同
猜你喜欢
  • 2015-01-27
  • 1970-01-01
  • 1970-01-01
  • 2019-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多