我在我的应用中添加了一个浮动操作按钮,我有一个材料设计主题并将其设置为Android Manifest,我已经将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;
}
}
}
这是我的主题:
<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 Manifest:
android:theme="@style/AppTheme"
答案 0 :(得分:4)
您需要在此活动中使用Theme.AppCompat主题(或后代)。
有两种解决方案可以解决您的问题:
Activity
(所有活动)Manifest.xml
主题
Theme
。{/ li>中添加Activity
属性
在Activity
(所有活动)中的应用范围中设置Manifest.xml
主题:
<application
android:label="@string/app_name"
android:icon="@drawable/Icon"
android:theme="@style/AppTheme">
</application>
在Xamarin.Android中,建议将活动主题添加为属性。您可以阅读文档Material Theme:
要主题化活动,请将主题设置添加到活动声明上方的[活动]属性,并将主题分配给要使用的材质主题风格。以下示例使用Theme.Material.Light主题活动:
例如:
[Activity(Label = "Muzzillo_v1", MainLauncher = true, Theme = "@style/AppTheme")]
public class MainActivity : AppCompatActivity