“您需要在此活动中使用 Theme.AppCompat 主题(或后代)。” 沙马林

5 xamarin.android floating-action-button

我正在向我的应用程序添加一个浮动操作按钮,我有一个材料设计主题并将其设置到 Android 清单中,我已经将 AppCompatActivity 继承设置为 MainActivity 但是如果我运行我的应用程序我有一个例外说“你需要在此活动中使用 Theme.AppCompat 主题(或后代)。”

这是我的主要活动:

using System;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V4.View;
using Android.Support.V7.App;
using Android.Views;

namespace M_v1
{
    [Activity(Label = "Muzzillo_v1", MainLauncher = true)]
    public class MainActivity : AppCompatActivity
    {
        Android.Support.V7.Widget.Toolbar toolbar;
        private FloatingActionButton fabMain;
        private View bgFabMenu;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            TabLayout tabLayout = (TabLayout)FindViewById(Resource.Id.tablayout_navigation);

            ViewPager viewPager = (ViewPager)FindViewById(Resource.Id.pager);
            SetupviewPager(viewPager);

            fabMain = FindViewById<FloatingActionButton>(Resource.Id.fab);
            bgFabMenu = FindViewById<View>(Resource.Id.bg_fab_menu);

            fabMain.Click += FabMain_Click;

            tabLayout.SetupWithViewPager(viewPager);

            toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);

            toolbar.Title = "App_v1";
        }

        private void FabMain_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private void SetupviewPager(ViewPager viewPager)
        {
            viewPager.OffscreenPageLimit = 2;

            PageAdapter adapter = new PageAdapter(SupportFragmentManager);
            adapter.AddFragment(new Fragment1(), "One");
            adapter.AddFragment(new Fragment2(), "Two");

            viewPager.Adapter = adapter;
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的主题:

<resources>
  <style  name = "AppTheme.Base" parent = "Theme.AppCompat.Light.NoActionBar">
    <item name ="colorPrimary">@color/primary</item>
    <item name ="colorPrimaryDark">@color/primaryDark</item>
    <item name="android:windowBackground">@color/window_background</item>
    <item name="windowActionModeOverlay">true</item>
  </style>

  <style name="AppTheme" parent="AppTheme.Base">
  </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

安卓清单:

android:theme="@style/AppTheme"
Run Code Online (Sandbox Code Playgroud)

Yor*_*hen 6

您需要在此活动中使用 Theme.AppCompat 主题(或后代)。

有两种解决方案可以解决您的问题:

  • Activity在应用范围中设置主题Manifest.xml(所有活动)
  • 或者ThemeActivity.

1.应用程序主题化

Activity在应用范围中设置主题Manifest.xml(所有活动):

<application 
    android:label="@string/app_name" 
    android:icon="@drawable/Icon" 
    android:theme="@style/AppTheme">
</application>
Run Code Online (Sandbox Code Playgroud)

2.活动主题

在 Xamarin.Android 中,建议将 Activity 主题添加为属性。您可以阅读文档Material Theme

要为活动设置主题,您可以在活动声明上方的 [Activity] 属性中添加主题设置,并将主题分配给您要使用的材料主题风格。以下示例使用 Theme.Material.Light 将活动主题化:

例如 :

[Activity(Label = "Muzzillo_v1", MainLauncher = true, Theme = "@style/AppTheme")]
public class MainActivity : AppCompatActivity
Run Code Online (Sandbox Code Playgroud)