“您需要在此活动中使用 Theme.AppCompat 主题(或后代)."沙马林 [英] "You need to use a Theme.AppCompat theme (or descendant) with this activity." Xamarin

查看:29
本文介绍了“您需要在此活动中使用 Theme.AppCompat 主题(或后代)."沙马林的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm adding a floating action button to my app, I have a material design theme and set it into the Android Manifest, I've already set the AppCompatActivity inheritance to the MainActivity but if I run my app I have an exception says " You need to use a Theme.AppCompat theme (or descendant) with this activity."

这是我的主要活动:

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

    }
}

这是我的主题:

<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>

Android 清单:

Android Manifest:

android:theme="@style/AppTheme"

推荐答案

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

You need to use a Theme.AppCompat theme (or descendant) with this activity.

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

There are two solutions to solve your problem :

  • Manifest.xml中的应用范围内设置Activity主题(所有活动)
  • 或者在Activity中添加一个Theme属性.
  • Set Activity theme in Application scope in Manifest.xml (all activities)
  • Or add a Theme attribute in Activity.

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

Set Activity theme in Application scope in Manifest.xml (all activities) :

<application 
    android:label="@string/app_name" 
    android:icon="@drawable/Icon" 
    android:theme="@style/AppTheme">
</application>

2.为活动设置主题

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

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

To theme an activity, you add a Theme setting to the [Activity] attribute above your activity declaration and assign Theme to the Material Theme flavor that you want to use. The following example themes an activity with Theme.Material.Light:

例如:

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

这篇关于“您需要在此活动中使用 Theme.AppCompat 主题(或后代)."沙马林的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆